zoukankan      html  css  js  c++  java
  • On jQuery

    http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/

    /*******************

    The short version is: a function invocation like fn(...args) is the same as fn.call(window [ES5-strict: undefined], ...args).

    Note that this is also true about functions declared inline: (function() {})() is the same as (function() {}).call(window [ES5-strict: undefined).

    **********************/

    Because jQuery makes such heavy use of anonymous callback functions, it uses the call method internally to set the this value of those callbacks to a more useful value. For instance, instead of receiving window as this in all event handlers (as you would without special intervention), jQuery invokes call on the callback with the element that set up the event handler as its first parameter.

    This is extremely useful, because the default value of this in anonymous callbacks is not particularly useful, but it can give beginners to JavaScript the impression that this is, in general a strange, often mutated concept that is hard to reason about.

    If you understand the basic rules for converting a sugary function call into a desugared func.call(thisValue, ...args), you should be able to navigate the not so treacherous waters of the JavaScript this value.

    this-table.png

  • 相关阅读:
    Web的攻击技术
    基于HTTP的功能追加协议
    确认访问用户身份的认证
    基本数据结构的模拟
    BFS与DFS
    KMP算法
    Trie字典树
    C++的结构体使用
    C++入门(命名空间)
    算法:C++入门
  • 原文地址:https://www.cnblogs.com/zjsct/p/3598669.html
Copyright © 2011-2022 走看看