javascript 如何在原型函数中访问javascript对象变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6784927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to access javascript object variables in prototype function
提问by David Kethel
I have the following javascript
我有以下 javascript
function person() {
//private Variable
var fName = null;
var lName = null;
// assign value to private variable
fName = "Dave";
lName = "Smith";
};
person.prototype.fullName = function () {
return this.fName + " " + this.lName;
};
var myPerson = new person();
alert(myPerson.fullName());
I am trying to get an understanding of object orientated techniques in javascript. I have a simple person object and added a function to its prototype.
我试图了解 javascript 中的面向对象技术。我有一个简单的 person 对象,并在它的原型中添加了一个函数。
I was expecting the alert to have "Dave Smith", however I got "underfined underfined"
. why is that and how do I fix it?
我期待警报有“戴夫史密斯”,但是我得到了"underfined underfined"
. 为什么会这样,我该如何解决?
回答by roberkules
Unfortunately you can't access a private variable. So either you change it to a public property or you add getter/setter methods.
不幸的是,您无法访问私有变量。因此,要么将其更改为公共属性,要么添加 getter/setter 方法。
function person() {
//private Variable
var fName = null;
var lName = null;
// assign value to private variable
fName = "Dave";
lName = "Smith";
this.setFName = function(value){ fName = value; };
this.getFName = function(){ return fName; }
};
see javascript - accessing private member variables from prototype-defined functions
请参阅javascript - 从原型定义的函数访问私有成员变量
But actually this looks like what you are looking for: Javascript private member on prototype
但实际上这看起来像你正在寻找的: 原型上的 Javascript 私有成员
from that SO post:
从那个SO帖子:
As JavaScript is lexically scoped, you can simulate this on a per-object level by using the constructor function as a closure over your 'private members' and defining your methods in the constructor, but this won't work for methods defined in the constructor's prototype property.
由于 JavaScript 是词法范围的,您可以通过使用构造函数作为“私有成员”上的闭包并在构造函数中定义方法来在每个对象级别模拟这一点,但这不适用于在构造函数中定义的方法原型属性。
in your case:
在你的情况下:
var Person = (function() {
var store = {}, guid = 0;
function Person () {
this.__guid = ++guid;
store[guid] = {
fName: "Dave",
lName: "Smith"
};
}
Person.prototype.fullName = function() {
var privates = store[this.__guid];
return privates.fName + " " + privates.lName;
};
Person.prototype.destroy = function() {
delete store[this.__guid];
};
return Person;
})();
var myPerson = new Person();
alert(myPerson.fullName());
// in the end, destroy the instance to avoid a memory leak
myPerson.destroy();
Check out the live demo at http://jsfiddle.net/roberkules/xurHU/
回答by RobG
When you call person as a constructor, a new object is created as if by new Object()
and assigned to its thiskeyword. It is that object that will be returned by default from the constructor.
当您将 person 作为构造函数调用时,会像 by 一样创建一个新对象new Object()
并将其分配给它的this关键字。默认情况下,该对象将从构造函数返回。
So if you want your instance to have properties, you need to add them to that object:
因此,如果您希望实例具有属性,则需要将它们添加到该对象中:
function Person() {
// assign to public properties
this.fName = "Dave";
this.lName = "Smith";
};
Incidentally, by convention functions that are intended to be called as constructors are given a name starting with a capital letter.
顺便说一句,按照惯例,打算作为构造函数调用的函数的名称以大写字母开头。
回答by RobG
You're declaring those variables as local to the function, instead of making them part of the object. In order to put them in the instance, you've got to use 'this' in the constructor as well. For example:
您将这些变量声明为函数的局部变量,而不是使它们成为对象的一部分。为了将它们放入实例中,您还必须在构造函数中使用“this”。例如:
function person() {
this.fName = 'Dave';
this.lName = 'Smith';
}
person.prototype.fullName = function () {
return this.fName + " " + this.lName;
};
var myPerson = new person();
alert(myPerson.fullName());
回答by Justin Ethier
In the constructor you should assign your variables to this
:
在构造函数中,您应该将变量分配给this
:
this.fName = null;
this.lName = null;
But then they are not private. JavaScript does not have private variables like a "classic" Object Oriented language. The only "private" variables are local variables. An alternative to the above is to assign getter/setter methods to this
within the constructor.
但是他们不是私人的。JavaScript 没有像“经典”面向对象语言那样的私有变量。唯一的“私有”变量是局部变量。上面的替代方法是this
在构造函数中分配 getter/setter 方法。