[英]JavaScript - understand methods
我對理解方法原理有疑問。
我理解功能,我知道
function hello() {
alert('Hello World');
}
是相同的
var hello = function() {
alert('Hello World');
}
現在,我的問題是什么。
這是我的一個方法的對象。 我不明白為什么在yarsLeft
內部function people()
不需要括號
我正在尋找一個合乎邏輯的解釋。
function people(name, age) {
this.name = name;
this.age = age;
this.yearsUntilRetire = yearsLeft; // this is confised for me
}
function yearsLeft() {
var numYears = 65 - this.age;
return numYears;
}
創建對象
var test = new people('Superman', 50);
test.yearsUntilRetire(); // I understand this code and calling a method in that way
為什么我不能寫this.yearsUntilRetire() = yearsLeft
或this.yearsUntilRetire = yearsLeft();
當您使用沒有括號( ()
)的函數名稱時,您將設置對函數本身的引用。
當您使用與括號相同的功能時,您正在執行該功能 。
所以這一行
this.yearsUntilRetire = yearsLeft;
設置yearsUntilRetire
指向函數。 此后你可以這樣做:
this.yearsUntilRetire(); // execute the yearsLeft function.
為什么我不能寫這個.yearsUntilRetire()= yearsLeft或this.yearsUntilRetire = yearsLeft();
在第一種情況下,您不能將調用函數的結果設置為不同的結果 - 這是語法錯誤。
在第二種情況下,您可以 - 它將變量yearsUntilRetire
設置為調用函數yearsLeft
的結果
1) function hello() {alert('Hello World');}
與 var hello = function() {alert('Hello World');}
第一個是函數聲明。 第二個是分配給名為hello
的變量的函數表達式。 但是當然在使用這兩個函數時,您可以將其視為相同且兩者的行為相同。
2)函數將始終被調用,如函數名稱后跟括號,例如: yearsLeft()
。 這是調用函數的唯一方法。
函數是JS中的First Class對象。 它可以聲明,表達,分配,傳遞給另一個函數並從函數返回。
所以在你的情況下,你的意圖是將全局函數yearsLeft
object屬性yearsUntilRetire
。 你完成任何其他任務的方式 - 記住“ 函數是頭等對象 ”
這里有幾個工作示例來說明您的問題的答案。 請參閱內聯注釋以獲得解釋。
console.assert( typeof helloWorldAsVar === 'function', 'helloWorldAsVar is defined' ); console.log( helloWorldAsVar ); console.assert( typeof helloWorld === 'function', 'helloWorld is defined' ); // creating a function like helloWorld means it will be hoisted to the top of the file as js does two passes // when compiling the script. one to declare all the functions and initialize all the variables as undefined. // and another to run through the code. console.log(helloWorld); var helloWorldAsVar = function() { return 'this will not get hoisted, and the function name is anonymous'; } // helloWorldAsVar is only available after it is declared. console.assert(typeof helloWorldAsVar === 'function', 'helloWorldAsVar is defined'); console.log( helloWorldAsVar ); function helloWorld() { return 'this will get hoisted to the top of the function, and it will keep its name'; }
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"> </script>
// this is a constructor function, it is essentially a typed object that can // have it's own methods similar to an oop class in other languages like php or java // the convention is to name a constructor with the first letter capitalized and it should be singular function Person(name, age) { // here you are assigning properties to your People object this.name = name; this.age = age; // you are passing a reference to the yearsLeft function // this.yearsUntilRetire = yearsLeft; } // a more standard way to add the yearsLeft function is to // use the People.prototype object which get's methods delegated to it eg. Person.prototype.yearsUntilRetire = yearsLeft; function yearsLeft() { var numYears = 65 - this.age; return numYears; } // Create objects var superman = new Person('Clark', 50 ), batman = new Person('Bruice', 40 ); console.log( superman, 'yearsUntilRetire', superman.yearsUntilRetire() ); console.log( batman, 'yearsUntilRetire', batman.yearsUntilRetire() ); // Why I can 't write this.yearsUntilRetire() = yearsLeft or this.yearsUntilRetire = yearsLeft(); console.assert(this === window, 'this in the global context refers to the window object' ); console.assert(superman instanceof Person, 'superman is a Person' ); console.assert(batman instanceof Person, 'batman is a Person' );
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.