javascript中对象构造函数中的var属性与this.properties



对象构造函数中的"this.property"one_answers"var property"之间有什么区别吗?

示例:

var person = function(){
    var age;
    this.firstName;        
}

是。例如,如果实例化一个新的person,如下所示:

var p = new person();

您将能够从外部访问firstName变量,它将成为新对象的属性:

console.log(p.firstName); // whatever you assigned it to

但不是age变量,其范围仅限于函数体内部:

console.log(p.age); // undefined

this.property返回调用对象的属性。在这种情况下,调用person()函数。

var property只是定义一个变量,其作用域是函数person()

相关内容

最新更新