了解公共/私有实例变量 [英] Understanding public/private instance variables

查看:97
本文介绍了了解公共/私有实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于公共/私人方法的教程,并且无法理解差异。

I am reading a tutorial on public/private methods and can't make sense of the difference.

对于私有方法,它说:私有成员是由构造函数构成的。普通变量和构造函数的参数成为私有成员。

For a private method it says, "Private members are made by the constructor. Ordinary vars and parameters of the constructor becomes the private members."

function Container(param) {
    this.member = param;
    var secret = 3;
    var that = this;
}

对于公共方法,此技术通常用于初始化公共实例变量构造函数的这个变量用于向对象添加成员。

And for public methods, "this technique is usually used to initialize public instance variables. The constructor's this variable is used to add members to the object."

function Container(param) {
    this.member = param;
}

正如你所看到的,两个函数都有参数 paramater和 this.member = param; 。一个是私有实例变量,另一个是公共实例变量?

As you can see both functions have a params paramater and a this.member = param;. Yet one is a private instance variable and the other is a public instance variable?

推荐答案

了解闭包



打开一个新的函数体创建一个新的范围。这种范围在JS中称为闭包。在该范围内创建的变量可在其所有子范围中访问。这意味着任何 var - 创建的变量都将对子函数可见。在此示例中,myTemporaryVar可在subScope中访问。

Understanding closures

Opening a new function body creates a new scope. This kind of scope is called a closure in JS. Variables created within that scope are accessible in all its sub-scopes. This means any var-created variable will be made visible to sub-functions. In this example, myTemporaryVar is accessible within subScope.

function myParentScope() {
  var myTemporaryVar = "sample";

  function subScope() {
    console.log(myTemporaryVar);
  }

  return subScope();
}

使用 new的函数关键字,为当前实例创建一个新的闭包。在该构造函数中创建的任何函数都将保持对范围变量的访问。在下一个示例中,函数sayHi可以访问临时变量myName。

When you use a function with the new keyword, a new closure is created for the current instance. Any function created within that constructor will keep access to the scope variables. In the next example, the function sayHi can access the temporary variable myName.

function Person(name) {
  var myName = name;
  this.sayHi = function() {
    console.log("Hi, my name is " + myName + ".");
  };
}

p = new Person("Bob");
p.sayHi(); // Hi, my name is Bob.

实际上,传递的参数与 var - 创建变量。构造函数的参数可在任何子函数中访问。所以前面的例子可以简化为:

Actually, passed parameters are the same as var-created variables. The constructor's parameters are accessible within any sub-function. So the previous example can be reduced to:

function Person(name) {
  this.sayHi = function() {
    console.log("Hi, my name is " + name + ".");
  };
}

p = new Person("Bob");
p.sayHi(); // Hi, my name is Bob.

这是JavaScript的一个非常独特的功能,因为它意味着 var -created变量在函数结束后仍然存在。

This is a very unique feature of JavaScript because it means var-created variables still exist after the end of the function as long as there still is a way to access them.

可以滥用闭包来创建具有getter和setter函数的私有成员。

Closures can be "abused" to create private members with getter and setter functions.

function Person(name) {
  this.getName = function() {
    return name;
  };

  this.setName = function(newname) {
    name = newname;
  };
}

p = new Person("Bob");
console.log(p.getName()); // "Bob"
p.setName("Alice");
console.log(p.getName()); // "Alice"
p.name; // undefined



为什么这不是真正的隐私



必须在构造函数中创建getter和setter才能访问 var -variable。以通用原型扩展方式添加的方法无法访问它们。原型方法也必须使用setter和getter,这使得这些变量的隐私非常无用。

Why this is not true privacy

The getter and setter have to be created within the constructor in order to access the var-variable. Methods added in the common prototype-extension way can't access them. Prototype-methods have to use the setters and getters too, which makes the privacy of such variables quite useless.

Person.prototype.sayGoodMorning = function() {
  console.log("Good morning, my name is " + this.getName() + ".");
}

直接访问方法中变量的唯一方法是实际创建它在构造函数中。但是将所有方法放在构造函数中是非常低效的,因为将为每个实例创建方法的新副本。这就是为什么许多人更喜欢使用自定义表示法来识别潜在的私人成员。 Google JavaScript样式指南建议在变量名末尾添加下划线。

The only way to directly access the variable within a method is to actually create it in the constructor. But putting all methods inside the constructor is extremely inefficient as a new copy of the methods will be created for each instance. This is why many people prefer simply using custom notation to identify would-be private members. The Google JavaScript Style Guide recommends placing an underscore at the end of the variable name.

function Person(name) {
  this.name_ = name;
}

Person.prototype.getName = function() {
  return this.name_;
}

Person.prototype.setName = function(name) {
  this.name_ = name;
}

Person.prototype.sayGoodMorning = function() {
  console.log("Good morning, my name is " + this.name_ + ".");
}

程序员有责任不要愚蠢并且访问是私人会员。请注意,这与Crockford的观点完全矛盾,但每个观点都是自己的。我在Python之后学习了JS,所以下划线隐私对我来说就像是第二天性。

It is the responsibility of the programmer to not be stupid and access the would-be private members. Note that this goes in total contradiction with Crockford's opinion, but to each is own. I learned JS after Python, so the underscore privacy is like a second nature to me.

这篇关于了解公共/私有实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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