简体   繁体   English

JavaScript私有变量,函数

[英]JavaScript private variables, functions

Learning JavaScript on codecademy, have several questions about lesson 24/30 在Codecademy上学习JavaScript,对第24/30课有几个疑问

In the code below: 在下面的代码中:

  1. why does john.getBalance() (last line) not result in an error when the getBalance function was not declared as this.prototype.getBalance but as this.getBalance ? 为什么john.getBalance()最后一行)不会导致错误时,为getBalance功能未声明this.prototype.getBalancethis.getBalance I thought subclasses could only use the functions of superclasses if they were declared with "prototype". 我认为子类只有在用“原型”声明的情况下才能使用超类的功能。
  2. What is the difference between declaring the function this.getBalance vs Person.getBalance ? 声明函数this.getBalancePerson.getBalance什么区别?
  3. Is declaring the function inside vs outside the constructor just a convention or does it make a functional difference? 是在构造函数内部还是外部声明函数只是一个约定,还是在功能上有所不同?
  4. If bankBalance is private, how do I know what functions are allowed to access it? 如果bankBalance是私有的,我怎么知道允许哪些功能访问它? Why is getBalance in the code allowed to access bankBalance ? 为什么代码中的getBalance允许访问bankBalance

My code : 我的代码:

function Person(first,last,age) {
    this.firstname = first;
    this.lastname = last;
    this.age = age;
    var bankBalance = 7500;

    this.getBalance = function() {
        // your code should return the bankBalance
        return bankBalance;
    };
}

var john = new Person('John','Smith',30);
console.log(john.bankBalance); //undefined

// create a new variable myBalance that calls getBalance()
console.log(myBalance=john.getBalance());

1) why does john.getBalance() (last line) not result in an error when the getBalance function was not declared as this.prototype.getBalance but as this.getBalance ? 1)为什么john.getBalance()最后一行)不会导致错误时, getBalance功能未声明this.prototype.getBalancethis.getBalance I thought subclasses could only use the functions of superclasses if they were declared with "prototype". 我认为子类只有在用“原型”声明的情况下才能使用超类的功能。

As I said in my answer to your last question , there is only one set of properties. 就像我在回答上一个问题时所说的那样,只有一组属性。 Javascript will look first on the object itself and if no property match is found there, it will look on the prototype. Javascript将首先在对象本身上查找,如果在该对象上未找到属性匹配项,它将在原型上查找。 There is no separate set of properties that belong to subclass and separate set for superclass. 没有属于子类的单独属性集,对于超类没有单独的属性集。 There is one set of properties directly on the object and there is a defined lookup order (for how to search the prototypes) if no matching property is found directly the object itself, but it is logically one overall set of properties on the object (and a method is just a property with a function for its value). 如果没有直接在对象本身上找到匹配的属性,则直接在对象上有一组属性,并且有一个已定义的查找顺序(用于搜索原型),但是从逻辑上讲,这是对象上的一组整体属性(和方法只是具有其功能的属性)。 There is not one set of properties that belong to the superclass and one set to the subclass. 没有一组属于超类的属性,而没有一组属于子类的属性。 In Javascript you should not think of it that way (I know it feels like that in some other languages, but it isn't that way in javascript). 在Javascript中,您不应该这样想(我知道在其他一些语言中也是如此,但在JavaScript中不是那样)。

2) What is the difference between declaring the function this.getBalance vs Person.getBalance? 2)声明函数this.getBalance与Person.getBalance有什么区别?

this.getBalance refers to a method on a specific object. this.getBalance引用特定对象上的方法。

Person.getBalance refers to a property on the Person constructor which is not something you've declared. Person.getBalance引用Person构造函数上的属性,而您没有声明该属性。

If you mean the difference between declaring this.getBalance and Person.prototype.getBalance , then the main difference is where the function is found in the lookup order. 如果你的意思是声明的区别this.getBalancePerson.prototype.getBalance ,那么主要的区别是函数在查找顺序找到。 Both will be found on any Person object or any object derived from a Person object. 两者都可以在任何Person对象或从Person对象派生的任何对象上找到。 Technically it is more efficient for the runtime if you declare it on the prototype because there is only one shared instance of the function object rather than creating a new instance of the function object for each instance of Person , but operationally they are the same. 从技术上讲,如果在原型上声明运行时,则对于运行时而言效率更高,因为只有一个共享的功能对象实例,而不是为每个Person实例创建一个功能对象的新实例,但是在操作上它们是相同的。 Using the prototype is preferred unless there is a specific reason not to. 除非有特殊原因,否则最好使用原型。

3) Is declaring the function inside vs outside the constructor just a convention or does it make a functional difference? 3)是在构造函数内部还是外部声明函数只是一个约定,还是在功能上有所不同?

It is more efficient to declare methods on the prototype, but they can also be declared in the constructor and there is little operational difference except that methods declared in the constructor have access to local variables declared in the constructor which can be used as private instance variables. 在原型上声明方法更有效,但是它们也可以在构造函数中声明,除了在构造函数中声明的方法可以访问在构造函数中声明的可用作私有实例变量的局部变量外,操作上几乎没有区别。 Using the prototype is preferred unless there is a specific reason not to. 除非有特殊原因,否则最好使用原型。

4) If bankBalance is private, how do I know what functions are allowed to access it? 4)如果bankBalance是私有的,我怎么知道允许哪些功能访问它? Why is getBalance in the code allowed to access bankBalance? 为什么代码中的getBalance允许访问bankBalance?

Only functions declared within the constructor (where bankBalance is declared) can access it. 只有在构造函数中声明的函数(声明了bankBalance的函数)才能访问它。 This is just plain javascript scoping rules, but comes in handy if you want to implement a private instance variable like this. 这只是普通的javascript作用域规则,但是如果您要实现这样的私有实例变量,它会派上用场。

  1. this.getBalance exists in every instance, while Person.prototype.getBalance only on the Person class (function). this.getBalance存在于每一个实例,而Person.prototype.getBalance仅在Person类(函数)。 In JavaScript, the inheritance is different from the traditional OOP language. 在JavaScript中,继承与传统的OOP语言不同。

  2. In your example, you cannot invoke Person.getBalance , no such function is declared. 在您的示例中,您无法调用Person.getBalance ,没有声明此类函数。

  3. Different. 不同。

  4. In JavaScript, there is no private fields. 在JavaScript中,没有private字段。 You only can use closure to simulate this concept. 您只能使用闭包来模拟此概念。

Let's go over your questions in order. 让我们按顺序浏览您的问题。

  1. getBalance() is defined on the Person object as an instance method. Person对象上将getBalance()定义为实例方法。 That simply means that every Person has its own getBalance() method. 这只是意味着每个Person都有自己的getBalance()方法。

  2. This ties in with the above. 这与以上联系在一起。 If you had declared the method on Person.prototype , it would be shared across all Person objects you create, whereas now each Person has its own getBalance() method. 如果已在Person.prototype上声明了该方法,则该方法将在您创建的所有Person对象之间共享,而现在每个Person都有自己的getBalance()方法。 The prototype approach can often provide a performance benefit for this reason. 因此,原型方法通常可以提供性能优势。

  3. Not sure what you mean here. 不知道你的意思在这里。 Feel free to provide clarification and I'll try to answer. 随时提供澄清,我会尽力回答。

  4. A function declared inside another function has access to variables declared in the outer function, so getBalance() therefore has access to bankBalance . 在另一个函数内部声明的函数可以访问在外部函数中声明的变量,因此getBalance()可以访问bankBalance But since bankBalance isn't a property of the Person object, you don't have access to it via john.bankBalance , which is why that is undefined . 但是由于bankBalance不是Person对象的属性,因此您无法通过john.bankBalance访问它,这就是undefined

  1. There is no difference 没有区别

  2. Yes, it will be global or local 是的,它将是全球性或本地性

  3. There is no allowance 没有津贴

  4. It's all the same 全部都是一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM