[英]Javascript closures returns NaN?
Trying to follow example in "Javascript: The Good Parts", creating a closure to be achieve hiding. 尝试遵循“ Javascript:好的部分”中的示例,创建一个实现隐藏的闭包。 In the case a simple "account" object which has a balance that can't be changed directly: 如果是一个简单的“帐户”对象,其余额无法直接更改:
<head>
<script>
var myapp = {};
var account = function() {
var value = 0;
return {
"account_name": "",
deposit: function (amount) {
value = this.value + amount;
},
withdrawal: function (amount) {
value = this.value - amount;
},
balance: function( ) {
return value;
}
}
}();
myapp.account = account;
</script>
</head>
<body>
<script>
myapp.account.account_name = "Fred";
myapp.account.deposit(100);
myapp.account.withdrawal(25);
document.writeln("<p>Account name = " + myapp.account.account_name + "</p>");
document.writeln("<p>Balance = " + myapp.account.balance( ) + "</p>");
</script>
</body>
which yields the following: 结果如下:
Account name = Fred 帐户名= Fred
Balance = NaN 余额= NaN
Wny NaN? 不知道吗?
That's happening because you never set this.value
. 发生这种情况是因为您从未设置过this.value
。 Basically, this can be fixed to just reference the closure'd variable value
: 基本上,可以将其固定为仅引用闭包的变量value
:
var myapp = {};
var account = function() {
var value = 0;
return {
"account_name": "",
deposit: function (amount) {
value = value + amount;
},
withdrawal: function (amount) {
value = value - amount;
},
balance: function( ) {
return value;
}
}
}();
myapp.account = account;
myapp.account.account_name = "Fred";
myapp.account.deposit(100);
myapp.account.withdrawal(25);
$('p:nth-child(1)').text('Account name = ' + myapp.account.account_name);
$('p:nth-child(2)').text('Balance = ' + myapp.account.balance());
Try it: i'm changed little bit your code.... this.value change into only value.. 尝试一下:我对您的代码进行了少许更改。
<head>
<script>
var myapp = {};
var account = function() {
var value = 0;
return {
"account_name": "",
deposit: function (amount) {
value = value + amount;
},
withdrawal: function (amount) {
value = value - amount;
},
balance: function( ) {
return value;
}
}
}();
myapp.account = account;
</script>
</head>
<body>
<script>
myapp.account.account_name = "Fred";
myapp.account.deposit(100);
myapp.account.withdrawal(25);
document.writeln("<p>Account name = " + myapp.account.account_name + "</p>");
document.writeln("<p>Balance = " + myapp.account.balance( ) + "</p>");
</script>
</body>
when you assign account
to myapp
, the scope
is missing, put var value
outside the function 当您将account
分配给myapp
, scope
丢失,请将var value
放在函数外部
var myapp = {};
var value = 0;
var account = (function(val){
return{
value: val,
"account_name": "",
deposit: function (amount) {
value = this.value + amount;
},
withdrawal: function (amount) {
value = this.value - amount;
},
balance: function( ) {
return value;
}
}
})(value);
console.log(account);
myapp.account = account;
myapp.account.account_name = "Fred";
myapp.account.deposit(100);
myapp.account.withdrawal(25);
console.log("<p>Account name = " + myapp.account.account_name + "</p>");
console.log("<p>Balance = " + myapp.account.balance( ) + "</p>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.