試圖找出一個主題,我無法縫合讓它工作。這裏是代碼:動態財產訪問
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Experiments</title>
</head>
<body>
<script>
var AGE = (function(){
var saying1 = "this is saying 1 ";
var saying2 = "this is saying 2 ";
var saying3 = "this is saying 3 ";
return {
say: function(numbers){
return this["saying" + numbers];
},
sayFunction: function(){
return "Hello World ";
}
};
})();
document.write(AGE.sayFunction());
document.write(AGE.say(1));
document.write(AGE.say(2));
document.write(AGE.say(3));
</script>
</body>
</html>
這不縫工作,試圖取代「返回這個[」說「+數字];」用「返回AGE [」說「+數字」);「和「return [」說「+數字」);「任何人都知道我失蹤或弄亂了什麼?
var AGE = (function() {
return {
saying1: "this is saying 1",
saying2: "this is saying 2 ",
saying3: "this is saying 3 ",
say: function(numbers) {
return this["saying" + numbers];
},
sayFunction: function() {
return "Hello World ";
}
};
})();
console.log(AGE.sayFunction());
console.log(AGE.say(1));
console.log(AGE.say(2));
console.log(AGE.say(3));
謝謝保羅現在唯一的問題是說1,說2和說3現在是公開的。
var AGE = (function(){
var say1 = "this is saying 1 ";
var say2 = "this is saying 2 ";
var say3 = "this is saying 3 ";
return {
say: function(){
return say1;
}
};
})();
document.write(AGE.say());
這就是我想實現但括號記號我現在不知道是否「動態屬性訪問權限」,可以通過公共職能與私人訪問工作有何影響?
'this'在您使用它的上下文中無效。更重要的是,你似乎對JS中原型/類的工作方式感到困惑。我的建議是在網上搜索一些教程,你可能會自己想出來。 – doogle