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。

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 passedthis
in the above example) defines the value ofthis
when evaluating the computed observable. Without passing it in, it would not have been possible to refer tothis.firstName()
orthis.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 forthis
, 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
。

TA貢獻1853條經(jīng)驗 獲得超6個贊
這里是指向?qū)嶓w對象的
而且這里AppViewModel其實是定義了一個類似類的東西,它可以new出一個對象來,這個對象就可以有具體的一些屬性了。

TA貢獻1811條經(jīng)驗 獲得超5個贊
我回一個沒有文檔時怎么判斷吧,畢竟this和函數(shù)call apply bind也有關(guān)
一個debugger就知道了
this.__proto__ === ko.constructor.prototype;
this.__proto__ === AppViewModel.prototype;
添加回答
舉報