0

Hi i am trying to get my head around some basic stuff in javascript, i want to create the equivalent to a class see below:

var myTest = {
  myTester:"testing",
  testCode:function(){
    return myTester;
 }
};

when i call alert(myTest.testCode()); i get error myTester is undefined;

i also have similar trouble when trying to set the value of myTester too, what i am trying to achieve here is something along the lines of this:

var myObj = myTest.testCode();
var tester = myObj.myTester;

as myObj is an object i should once created be able to access its values but i dont usually do javascript just jQuery and i am trying to create an application in pure javascript just for brain feed and would appreciate a little guidence, especially on what do you actually call this, is it a class????

thanks

2 Answers 2

5

You have to access this:

var myTest = {
  myTester:"testing",
  testCode:function(){
    return this.myTester;
 }
};

Explanation: In each function, this refers to the context the function is called in. If you call a function with func() then this will refer to the global object (window in browser).
If you call obj.func() then this will refer to obj.

This is the only connection between a function and an object. The mere fact that you define a function as property of an object does not make the function aware of that object.

Functions are first class objects in JavaScript and as such not bound to any object (or class) (like in other languages, e.g. Java).


But

var myObj = myTest.testCode();
var tester = myObj.myTester;

would not work, as myTest.testCode() returns the string "testing" (and strings don't have a myTester property). If you want to make this code work, you would have to return this in testCode:

var myTest = {
  myTester:"testing",
  testCode:function(){
    return this;
 }
};

but then, the two lines are identical to

var tester = myTest.myTester;

I suggest to read some introduction to JavaScript, e.g. MDC - JavaScript Guide and read especially about objects.

P.S.: To be very correct, classes don't exist in JavaScript. Only objects and constructor functions.

Sign up to request clarification or add additional context in comments.

3 Comments

how can i get multiple values as above may be a bum example, i.e myTester:null, myTester2:null,testCode()function(){ this.myTester="123"; this.myTester2 = "456"; } How would i get myTester and myTester2's value which would depend on what happened in testcode if you get my meaning thanks
@minus4: Well, if your object is stored in var myTest = {}. Then you would do: myTest.testCode(); and after that you can access the values via myTest.myTester or myTest.myTester2. But you don't need to store the return value of testCode() as you don't return anything anyway.
wicked yep tried that its all comming together :-) thanks pal
0

What you have in your example wouldn't be called a class, in my opinion, as it has no constructor function. At this point, it's just an object.

As mentioned by Felix Kling, you need to explicitly use this to refer to properties of your object from within its methods.

This example will not work to do what you want:

var myObj = myTest.testCode();
var tester = myObj.myTester;

The testCode method (once fixed to use this), returns the myTester property (a string). That string doesn't itself have a myTester property, so the tester variable will contain undefined.

You could instead just do

var tester = myTest.myTester;

To step back, the general pattern for a class could be something like this:

MyClass = function(initialValue) {
  this._myProperty = initialValue;
}

MyClass.prototype = {
  getMyProperty: function() {
    return this._myProperty;
  },

  setMyProperty: function(value) {
    this._myProperty = value;
  }
};

var myObj = new MyClass("test");
alert(myObj.getMyProperty());
myObj.setMyProperty("something");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.