gpt4 book ai didi

JavaScript 变量对象

转载 作者:行者123 更新时间:2023-12-03 03:47:25 24 4
gpt4 key购买 nike

我对 Javascript 很陌生请更正我的代码或建议最佳选择

var myApp = {};

myApp.set = function(VAL) {
myApp.id = VAL;
}

myApp.get = function() {
return myApp.id;
}

function PageLoad() {
var X = new myApp;
var Y = new myApp;

Y.set(20);
X.set(10);

alert(X.get());
alert(Y.get());
}

它仅显示最后一个分配值,即 10 :(

提前致谢马诺杰

最佳答案

@Manoj Chavanke先生请使用此代码

 function myApp(){
}
myApp.prototype.set = function(VAL) {
this.id = VAL;
}

myApp.prototype.get = function() {
return this.id;
}

function PageLoad() {
var X = new myApp; console.log(X);
var Y = new myApp; console.log(Y);

Y.set(20);
X.set(10);

alert(X.get());
alert(Y.get());
}
PageLoad();

关于JavaScript 变量对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45319559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com