鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 關(guān)于function里的this問題

關(guān)于function里的this問題

我是js新人,這是knockout.js里的一個(gè)示例

html

<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<p>Full name: <strong data-bind="text: fullName"></strong></p>

Js

function AppViewModel() {
    this.firstName = ko.observable("Bert");
    this.lastName = ko.observable("Bertington");
    
    this.fullName = ko.computed(function(){
        return this.firstName() + " " + this.lastName();
    },this);
}

// Activates knockout.js
ko.applyBindings(new AppViewModel());

我有點(diǎn)不太理解fullName里function最后的this是什么作用,function的參數(shù)?還是別的?
它指向的是誰?該怎么可以透徹的理解這個(gè)語句?
我經(jīng)常對(duì)匿名function(){}后面直接加參數(shù)這種行為理解不能,有沒有什么好的文章介紹相關(guān)的內(nèi)容?

謝謝大家

回答
編輯回答
病癮

1.要理解這里的this指向哪里,只需要理解函數(shù)作為構(gòu)造函數(shù)之后,也就是放在new 關(guān)鍵字之后發(fā)生了什么
2.其實(shí)這里的this是指向AppViewModel new出來的對(duì)象,可以參考https://developer.mozilla.org...
3.匿名函數(shù)就是一個(gè)函數(shù),只不過這個(gè)函數(shù)不需要在其他地方調(diào)用,所以不需要有一個(gè)名字,在JavaScript里函數(shù)是一等公民,也是對(duì)象,可以作為參數(shù)傳遞

2017年8月20日 06:53
編輯回答
陪妳哭

兄弟那個(gè)函數(shù)叫自執(zhí)行函數(shù) 格式為(function(){})()這是一般格式 現(xiàn)在有es6了 所以也可以寫成:
(()=>{})()箭頭函數(shù)。 至于你說的有關(guān)于this指向的問題,我覺得這個(gè)你得先系統(tǒng)的去學(xué)this有指向window的,也有指向當(dāng)前對(duì)象的,還有指向當(dāng)前函數(shù)的,在回調(diào)函數(shù)中的this指向window。

2017年10月23日 17:28
編輯回答
礙你眼

關(guān)于 this,我已經(jīng)在JavaScript 的 this 指向問題深度解析 進(jìn)行了詳細(xì)的講述,所以這里就不重復(fù)了。

具體到這個(gè)問題,是關(guān)于 ko.computed() 的 API,看看從官方文檔摘取的一段:

Managing ‘this’

The second parameter to ko.computed (the bit where we passed this in the above example) defines the value of this when evaluating the computed observable. Without passing it in, it would not have been possible to refer to this.firstName() or this.lastName(). Experienced JavaScript coders will regard this as obvious, but if you’re still getting to know JavaScript it might seem strange. (Languages like C# and Java never expect the programmer to set a value for this, but JavaScript does, because its functions themselves aren’t part of any object by default.)

大致翻譯一下第一句就是:ko.computed 的第二個(gè)參數(shù)指定在執(zhí)行 computed 觀察函數(shù)時(shí)的 this

所以在 ko.computed(function() {...}, this) 這里傳入的 this,就是在 function() {...} 使用的 this,即 this.firstName()this.secondName() 的那個(gè) this。也就是 AppViewModel 作用域中的 this。

2018年8月23日 09:59
編輯回答
選擇

我回一個(gè)沒有文檔時(shí)怎么判斷吧,畢竟this和函數(shù)call apply bind也有關(guān)
一個(gè)debugger就知道了
this.__proto__ === ko.constructor.prototype;
this.__proto__ === AppViewModel.prototype;

2018年5月16日 17:57
編輯回答
心悲涼

這個(gè)其實(shí)是js函數(shù)基礎(chǔ),你需要一本紅寶書(Javascript高級(jí)程序設(shè)計(jì)),F(xiàn)unction 部分

2018年5月15日 21:10
編輯回答
心上人

這里是指向?qū)嶓w對(duì)象的
而且這里AppViewModel其實(shí)是定義了一個(gè)類似類的東西,它可以new出一個(gè)對(duì)象來,這個(gè)對(duì)象就可以有具體的一些屬性了。

2017年9月7日 23:45
編輯回答
尛憇藌

// ko.computed大概是這樣執(zhí)行的
ko.computed = function(fn, context) {
    return fn.call(context)
}

this.fullName = ko.computed(function(){
    return this.firstName() + " " + this.lastName();
}, this);

computed的第二個(gè)參數(shù)可以讓我們指定上下文

2017年8月11日 04:08