如果一个变量被包含,并且一个实例设置了一个具有相同名称的属性,属性在哪里? [英] If a variable is enclosed, and an instance sets a property with the same name, where does the property go?

查看:168
本文介绍了如果一个变量被包含,并且一个实例设置了一个具有相同名称的属性,属性在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们大多数人都知道JavaScript没有私有属性的感觉,但是这种行为可以通过使用闭包来模拟:

  var Car = function(){
var name ='Tesla';
var wheelCount = 4

this.getName = function(){
return name;
};
this.getWheelCount = function(){
return wheelCount;
};
this.setName = function(newName){
name = newName;
};
this.setWheelCount = function(newCount){
wheelCount = newCount;
};
};

var myCar = new Car();
console.log(myCar.name); // undefined

这对于我们知道JavaScript闭包是如何工作是有意义的。但是,以下操作将有效:

  myCar.name ='Corvette'; 
console.log(myCar.name); //'Corvette'

但是如果从原型调用函数,使用

  console.log(myCar.getName()); //'Tesla'

当你调用 myCar.name ,您要向对象添加一个新属性。当在 getName (etc)函数时,仅仅是使用 this > Car 定义,以区分 Car 声明中的名称 myCar.name



我有一种感觉,我知道答案,但我想某些。此外,我认为这对于其他人如何掌握JavaScript框架如何工作是非常有价值的。

解决方案


当声明 getName (etc)函数时,只是使用 this code> Car 定义,以区分 Car 声明中的封闭名称与 myCar.name


$ b

c $ c>此
(或 myCar )是对象,您可以将属性有它(在这种情况下所有这些方法)。如果要使用属性,则必须使用属性访问器。在构造函数中



var name 只是一个本地



或者,回答你的标题问题:


如果一个变量被包含,并且一个实例设置了一个具有相同名称的属性,属性在哪里?


属性在对象上 。它不会完全干扰位于 Car 构造函数调用的范围中的变量。


Most of us know that JavaScript has no sense of "private" properties, but that behavior can be emulated through the use of closures:

var Car = function () {
    var name = 'Tesla';
    var wheelCount = 4;

    this.getName = function () {
        return name;
    };
    this.getWheelCount = function () {
        return wheelCount;
    };
    this.setName = function (newName) {
        name = newName;
    };
    this.setWheelCount = function (newCount) {
        wheelCount = newCount;
    };
};

var myCar = new Car();
console.log(myCar.name); // undefined

That all makes sense to us that know about how JavaScript closures work. However, the following will work:

myCar.name = 'Corvette';
console.log(myCar.name); // 'Corvette'

But then if you call the function from the prototype, the enclosed variable is still used

console.log(myCar.getName()); // 'Tesla'

When you call myCar.name, you're adding a new property to the object. Is it just the entire point of using this when declaring the getName (etc) functions inside the Car definition to make the differentiation between the enclosed name inside the Car declaration versus the myCar.name?

I have a feeling that I know the answer to this, but I would like to be certain. Plus, I think this would be something invaluable to other people getting an advanced grasp on how JavaScript enclosures work.

解决方案

Is it just the entire point of using this when declaring the getName (etc) functions inside the Car definition to make the differentiation between the enclosed name inside the Car declaration versus the myCar.name?

Yes.

this (or myCar) is the object, on which you put the properties that you want to have it (in this case all those methods). If you want to use properties, you must use a property accessor.

var name is just a local variable in the constructor function, which is accessed by closure from the object's methods.

Or, to answer your title question:

If a variable is enclosed, and an instance sets a property with the same name, where does the property go?

The property goes on the object. It doesn't interfere at all with the variable, which is located in the scope of the Car constructor invocation.

这篇关于如果一个变量被包含,并且一个实例设置了一个具有相同名称的属性,属性在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆