davethecoder
davethecoder

Reputation: 3932

object returns not defined in javascript

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

Upvotes: 0

Views: 8198

Answers (2)

jhurshman
jhurshman

Reputation: 6067

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");

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 817030

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.

Upvotes: 5

Related Questions