function get_denomination(){
return "Fabrice Petard";
}
function auto_flatterie(flatterie){ //
return this.get_denomination() + " est " + flatterie + " !";
}
console.debug(auto_flatterie("le plus intelligent")); // affiche "Fabrice Petard est le plus intelligent !"
你能解釋一下「this」綁定到什麼,以及爲什麼this.get_denomination不會拋出任何錯誤?請解釋爲什麼這個奇怪的javascript代碼工作
請閱讀https://developer.mozilla.org/en/JavaScript/Reference/Operators/this。 – 2012-07-29 14:34:11
這可能對您有所幫助http://www.robertsosinski.com/2009/04/28/binding-scope-in-javascript/,它正在討論您正在描述的案例 – Adi 2012-07-29 14:36:23
您可能想要在['嚴格模式'](https://developer.mozilla.org/en/JavaScript/Strict_mode),其中[此代碼實際上會引發錯誤](http://jsfiddle.net/mYecU/)。嚴格模式還可以保護您免受諸如隱式全局變量等其他糟糕的語言決定。 – Esailija 2012-07-29 14:43:46