最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

object returns not defined in javascript - Stack Overflow

programmeradmin2浏览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

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

Share Improve this question asked Dec 17, 2010 at 14:43 davethecoderdavethecoder 3,9324 gold badges37 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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.

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");
发布评论

评论列表(0)

  1. 暂无评论