2016-01-11 140 views
4

我对理解方法的原理有所怀疑。
我理解的功能,我知道JavaScript - 了解方法

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() = yearsLeftthis.yearsUntilRetire = yearsLeft();

+1

这是一个函数引用。函数可以像JS中的任何其他类型一样引用和传递。 – Teemu

+1

'()'将调用(调用)该函数。您正在将函数表达式设置为'this.yearsUntilRetire'。一旦你调用'this.yearsUntilRetire()',函数表达式就会被执行。 – Rayon

回答

7

当您使用函数的名称不使用括号(())要在基准设置为功能本身

当您使用括号同样的功能你是执行功能

因此这条线

this.yearsUntilRetire = yearsLeft; 

是设置yearsUntilRetire指向功能。此后,你可以这样做:

this.yearsUntilRetire(); // execute the yearsLeft function. 

为什么我不能写this.yearsUntilRetire()= yearsLeft或this.yearsUntilRetire = yearsLeft();

在第一种情况下,您不能将调用函数的结果设置为不同的结果 - 这是语法错误。

在第二种情况下,可以 - 它把变量yearsUntilRetire到调用函数yearsLeft

1

1)function hello() {alert('Hello World');} 的结果不相同var hello = function() {alert('Hello World');}

第一个是函数声明。第二个是分配给一个名为hello的变量的函数表达式。但是当然在使用这两个函数的时候,你可以把它看作相同,并且两者的行为都是相同的。

2)函数总是被称为函数名称,后面跟括号,例如:yearsLeft()。这就是调用函数的唯一方法。

函数是JS中的First Class对象。它可以被声明,表达,分配,传递给另一个函数并从一个函数返回。

所以在你的情况下,你的意图是将全局函数yearsLeft分配给对象属性yearsUntilRetire。你完成任何其他任务完成的方式 - 请记住“函数是头等对象

1

这里有几个工作示例来说明您的问题的答案。 请参阅内嵌评论以获取解释。

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>