- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
function wrap(func) {
console.log('0', this)
return function x() {
console.log('1', this)
func()
return function z() {
console.log('3', this)
}
}
}
var obj = {
x: 5,
test: wrap(function y() {
console.log('2', this)
})
}
obj.test()()
上面的代码记录
// 0 Window
// 1 Object
// 2 Window
// 3 Window
我无法理解什么决定了 this
的值应该是什么。
//0 Window
是有道理的,因为这是第一次调用 wrap 的时候,此时它的值 this
不应设置为 obj
//1 Object
有意义 obj.test
现在等于 function x()
,它正确地记录了 this
作为 obj
//2 Window
我不确定为什么会这样。为什么 this
的值不会传播到 func()
?我认为 this
应该是指函数的所有者。这是否意味着即使 function y()
是在 obj 内部创建的,它也会以某种方式被提升到窗口?
//3 Window
同样 function z()
为什么 this
的值没有从 function x( )
。 this
的值不应该在那里传播吗?
最佳答案
'this' 绑定(bind)非常困惑。但是,如果您记住以下这些规则,就会更容易理解。根据 Javascript 文档,“this”绑定(bind)的顺序有四个规则:
在您的脚本中,只有规则 #3 和 #4 适用,因为没有新的运算符和 call 或 apply 方法。
所以解释是:
//0 窗口 - 规则 #4 默认(全局)
//1 对象 - 规则 #3 与包含对象的隐式绑定(bind),如对象的方法,在您的情况下为 obj.test
//2 窗口 - 规则 #4 默认(全局)
//3 窗口 - 规则 #4 默认(全局)
关于javascript - 以下代码片段中 `this` 值变化的解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36586336/
这个问题在这里已经有了答案: standalone parentheses in javascript [duplicate] (5 个答案) 关闭 8 年前。 我在学习JavaScript,有时会
我是mysql新手,我必须减少以下更新查询的执行时间 UPDATE temp_countcalculations, ( SELECT count(*) as insuffcounts,CRP_
def escape_html(s): for (i, o) in (("&","&"),(">", ">"),(" "变成 ">"等。 关于python - 以下 for 循环
if (read(read(cin, data1), data2)) 问题是C++ Primer 5th Edition 的练习。 read 函数定义如下: std::istream &read(st
我想创建两个宏。其中一个将扩展到函数原型(prototype)和函数内容,另一个将扩展到仅函数原型(prototype)。我正在考虑创建以下内容: #ifdef SOME_CONDITION #def
我正在使用 jongo API - org.jongo.MongoCollection 是类。 我有对象 ID 列表并转换为与 ObjectId[] 相同并尝试按如下方式查询 collection.f
有人可以解释以下正则表达式匹配什么吗? ^.*$ 谢谢! 最佳答案 或者整个字符串或者整行,取决于是否multiline mode被使用。 关于java - 以下 ^.*$ 正则表达式匹配什么?,我们
#include void main() { int a,b,c; for(b = c = 10; a = "- FIGURE?, UMKC,XYZHello Folks,TFy!QJ
我的代码段中的以下代码行被 Sonar 检测为问题。 代码段: final int Pending=1; Sonar 问题: Name 'Pending' must matc
Print name of all activities with neither maximum nor minimum number of participants 我尝试了以下查询,但出现错误:
这个问题在这里已经有了答案: What is this practice called in JavaScript? (7 个回答) 关闭8年前。 (function() { //do stuff
根据任务,我们必须通过 foldr 实现 foldl。通过比较函数签名和 foldl 实现,我得到了以下解决方案: myFoldl :: (a -> b -> a) -> a -> [b] -> a
这个问题在这里已经有了答案: Export an es6 default class inline with definition or at end of file? (1 个回答) 关闭 2 年
据我了解,以下是相同的: Person p{}; // Case 1 Person p = {}; // Case 1.5 我注意到 Person p = Person{}; // Case 2 产生
below i have given a javascript code picture `` can any one help me in this code. what do this code.
我想在标题和正文上搜索全文,并在答案计数上进行过滤。 我阅读了elasticsearch documentation for combining filters并构建了此查询。 "query": {
它是流动的 C 代码中的内存泄漏吗? #include int *a; int main() { a = malloc(sizeof(int)*10); return
这两个声明有什么区别: char (*ptr)[N]; 对比 char ptr[][N]; 谢谢。 最佳答案 (1)声明 char (*ptr)[N]; ptr 是指向大小为 N 的字符数组的指针 下
data II = I Int Int deriving (Show) instance II Show where show I a b = show (a+b) showt.hs:3:2: s
我从 clojuredoc 中阅读了关于 condp 的文档。在文档中我找到了以下代码: (condp 一些 [1 2 3 4] #{0 6 7} :>> 公司 #{4 5 9} :>> 十二月 #{
我是一名优秀的程序员,十分优秀!