2015-12-25 26 views
0

我知道這是一個非常基本的問題,但由於我是初學者,所以對此知之甚少。我試圖通過搜索各種在線資源來了解它,但無法清楚地看到它。提前致謝。有人可以請一個真實的例子解釋我的功能和方法在JavaScript中的區別嗎?

+0

@duffymo:你如何看待它們並不重要。 [ES5](https://es5.github.io/#x4.3.27)和[ES6](http://www.ecma-international.org/ecma-262/6.0/#sec-method)規範如何認爲他們是重要的一點。而且有一個明顯的區別 - 只有方法可以使用點符號在目標上調用,在進程中設置'this'(或'self',用其他語言)。 – Amadan

+0

你是對的;我的想法在這裏並不重要。感謝您的指導。 – duffymo

回答

2

JavaScript中的方法是在對象屬性上設置的函數;不多也不少。

window.f = function() {} // method of `window` 

a = { 
    g: function() {} // method of `a` 
}; 

function x() { 
    var h = function() {} // not a method, because it's in a local variable, 
         // not in an object attribute 
    var b = { i: h };  // method of `b` now. 
}; 
相關問題