第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

Qyouu 2019-03-21 15:19:37
我是js新人,這是knockout.js里的一個示例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>Jsfunction AppViewModel() {    this.firstName = ko.observable("Bert");    this.lastName = ko.observable("Bertington");        this.fullName = ko.computed(function(){        return this.firstName() + " " + this.lastName();    },this);}// Activates knockout.jsko.applyBindings(new AppViewModel());我有點不太理解fullName里function最后的this是什么作用,function的參數(shù)?還是別的?它指向的是誰?該怎么可以透徹的理解這個語句?我經(jīng)常對匿名function(){}后面直接加參數(shù)這種行為理解不能,有沒有什么好的文章介紹相關(guān)的內(nèi)容?謝謝大家
查看完整描述

7 回答

?
千巷貓影

TA貢獻1829條經(jīng)驗 獲得超7個贊

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

查看完整回答
反對 回復(fù) 2019-04-08
?
桃花長相依

TA貢獻1860條經(jīng)驗 獲得超8個贊

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

具體到這個問題,是關(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 的第二個參數(shù)指定在執(zhí)行 computed 觀察函數(shù)時的 this。

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


查看完整回答
反對 回復(fù) 2019-04-08
?
墨色風(fēng)雨

TA貢獻1853條經(jīng)驗 獲得超6個贊

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

查看完整回答
反對 回復(fù) 2019-04-08
?
四季花海

TA貢獻1811條經(jīng)驗 獲得超5個贊

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

查看完整回答
反對 回復(fù) 2019-04-08
  • 7 回答
  • 0 關(guān)注
  • 807 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號