我正在通過CodeAcademy上的JavaScript軌道工作,並且遇到了對象II - >對象簡介II - >私有方法(25/30)的問題。我不瞭解指向事物背後的動機,指向其他事物。如果我們試圖獲得變量的價值,爲什麼不直接引用它呢?例如多層引用的動機? - Code Academy JavaScript
變種d是指無功ç 和 變種C指的變種b 和 變種b所VAR一個
爲什麼這樣做,而不是指一個變種開始?爲什麼要創建變量b,c和d?無論如何在私人方法 - 25/30問題的核心在下面。 *注 - 我把詳盡/確切的代碼在最底層
**var bankBalance = 7500;** // Located within the Person class
**var returnBalance = function()** // Create returnBalance method which returns
**return bankBalance;** // the bankBalance above
t**his.askTeller = function() {** // Create the askTeller method which calls
**return returnBalance;** // returnBalance method above
**var myBalanceMethod = john.askTeller();** // create a variable named myBalanceMethod
// which equals the value for askTeller method above
**var myBalance = myBalanceMethod();** // create a variable named myBalance which
// equals the value for myBalanceMethod method above
**console.log(myBalance);** // logs to the console the value of myBalance
這一切都似乎是一個很大的麻煩。經歷所有這些麻煩的動機是什麼,而不是更直接地引用bankBalance?
下面我已經包含了我的確切的代碼(注意 - 它工作得很好我只是不明白的多層背後的推理)
**function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
var returnBalance = function() {
return bankBalance;
};
// create the new function here
this.askTeller = function() {
return returnBalance;
};
}
var john = new Person('John','Smith',30);
console.log(john.returnBalance);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();
console.log(myBalance);**