[英]methods as properties of an object in javascript
i need to create a javascript function with a private variable that has setter and getter methods. 我需要创建一个具有setter和getter方法的私有变量的javascript函数。 i tried: 我试过了:
function createSecretHolder(secret) {
this._secret = secret;
var getSecret = function(){
return this._secret;
}
var setSecret = function(secret){
this._secret = secret;
}
}
and a version with: 并带有以下版本:
this.getSecret = function()...
and 和
this.seSecret = function()...
it is not passing the test suite on code wars. 它没有通过代码大战的测试套件。 something like 就像是
var obj = createSecretHolder(secret);
obj.getSecret();
obj.setSecret(newSecret);
and others which are hidden. 和其他隐藏的。 I get an error TypeError: Cannot read property 'getSecret' of undefined and another cannot call method setSecret 我收到错误TypeError:无法读取未定义的属性'getSecret',另一个无法调用方法setSecret
createSecretHolder()
doesn't return
a value so createSecretHolder(secret)
returns undefined
. createSecretHolder()
不return
值,因此createSecretHolder(secret)
返回undefined
。
So var obj = createSecretHolder(secret);
所以var obj = createSecretHolder(secret);
sets obj
to undefined. 将obj
设置为undefined。 Hence the error "Cannot read property 'getSecret' of undefined" when you try to access obj.getSecret()
. 因此,当您尝试访问obj.getSecret()
时出现错误“无法读取未定义的属性'getSecret'”。
Even if it returned an object, you have declared getSecret
using var
inside a function
. 即使它返回了一个对象,您也已在function
内部使用var
声明了getSecret
。 Variables described that way are scoped to the function. 以这种方式描述的变量仅限于该函数。 So when you try to access it outside the function, as you do in obj.getSecret()
, that won't work. 因此,当您尝试在函数外部访问它时(如在obj.getSecret()
所做的obj.getSecret()
,将无法正常工作。
You also seem to misunderstand how this
works. 您似乎也误解了this
工作原理。 It will not create a private variable. 它不会创建私有变量。
There are a number of ways to do this. 有很多方法可以做到这一点。 Here's one: 这是一个:
function createSecretHolder(mySecret) {
var secret = mySecret;
return {
getSecret: function(){
return secret;
},
setSecret: function (mySecret){
secret = mySecret;
}
};
}
When you use a Constructor to create an object you need to use the new
keyword. 使用构造函数创建对象时,需要使用new
关键字。
Inside your constructor you set a property with this._secret = secret
. 在构造函数内部,您可以使用this._secret = secret
设置属性。 This is accessible from outside. 可从外部访问。 It should be var _secret = secret
. 它应该是var _secret = secret
。
Also you create local functions inside your object to get/set _secret
. 您还可以在对象内部创建局部函数以获取/设置_secret
。 They should be methods of the object. 它们应该是对象的方法。 See below. 见下文。
// object constructor
function createSecretHolder(secret) {
// local variable. it isn't accessible from outside
var _secret = secret;
// method to get the secret
this.getSecret = function() {
return _secret;
}
// method to set the secret
this.setSecret = function(secret){
_secret = secret;
}
}
// create new "createSecretHolder" object
var secret = new createSecretHolder("secret 1");
secret.getSecret(); // returns "secret 1"
secret.setSecret("secret 2");
secret.getSecret(); // returns "secret 2"
Look into prototypejs OO programming. 研究prototypejs OO编程。
If you can't use prototypejs because of jQuery conflicts, there is a version of it only with OO support here: https://github.com/Prescia/Prototypejslt 如果由于jQuery冲突而无法使用prototypejs,则此处仅提供面向对象的版本: https : //github.com/Prescia/Prototypejslt
Your could would look like: 您可能会看起来像:
createSecretHolder= Class.create();
createSecretHolder.prototype = {
_secret: 0,
othervariable: true,
initialize: function(inSecret) { // this is the constructor on prototypejs
this._secret = inSecret;
}
}
// Create instance:
var myInstance = new createSecretHolder(5);
// so this should alert "5":
alert(myInstance._secret);
I can't live without prototypejs Object Orientation, so I often use this light version that won't conflict with other stuff 我不能没有prototypejs Object Orientation,所以我经常使用不会与其他东西冲突的轻量级版本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.