开发者

Why is 'this' always a Window in a prototype for Javascript Classes

开发者 https://www.devze.com 2023-03-08 13:35 出处:网络
Here is the code function Person(name, age, weight) { this._name = name; this._weight = weight; this._age = age;

Here is the code

function Person(name, age, weight) {
    this._name = name;
    this._weight = weight;
    this._age = age;
}

Person.prototype = {
    Anatomy: {
        Weight: this._weight,
        Height: (function () {
            //calculate height from age and weight
        })
    }
}

i expected Anatomy.weight to be 60 when i ran this code:

var x = new Person('jack',24,60);
console.dir(x.Anatomy);
开发者_开发知识库

To my surprise it was undefined. On inspection it seemed that this was referring to global object window. Now what has happened here :( I expected this._weight to refer Person objects weight, otherwise from rough calculation, this should have at the least referred to Anatomy since it is a object. Could someone clarify the doubt


You can't to that. this is only available in functions. Where you used it, it refers to the global object. A possible solution would be this:

function Anatomy(weight) {
    this.Weight = weight;
    this.Height = [...];
}

function Person(name, age, weight) {
    this._name = name;
    this._weight = weight;
    this._age = age;
    this.Anatomy = new Anatomy(this._weight);
}

I don't know if this is the best solution, but it's the best that I can think of right now.


this changes based on scope, and scope is only effected by functions. Thus, since Person.prototype is just an object that is not in a function, this refers to the global object, which in browsers tends to be window.

Edit: example fix

function Person(name, age, weight) {
    this._name = name;
    this._weight = weight;
    this._age = age;
    this.Anatomy: {
        Weight: this._weight,
        Height: (function () {
            //calculate height from age and weight
        })
    }
}


This has nothing to do with prototypes.

When you work in browser, your context (this) is set to window object. This allows you to call setTimeout and alert et al. as if they were global functions. I.e. any your script is a method of global window object.

This is not true in other Javascript hosts, e.g. in node.js.

0

精彩评论

暂无评论...
验证码 换一张
取 消