javascript closures and function contexts

Javascript has four kind of functions, namely 

  • global function 
  • constructor
  • prototype 
  • closure

I may be wrong about the types, but the rule of thumb is that the functions differs from each other in terms of how the 'this' pointer is determined.

 

another things that you might be aware is that , with the .call() and .apply() method, you can manipulate the context of a function. 

 

let's take an example. 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../unit.js" type="text/javascript"></script>

</head>
    <style type="text/css" >
      #results li.pass { color: Green }
      #results li.fail { color: Red }
    </style>

<body>
<button id="test">click me!</button>

<script type="text/javascript">

  function trigger(elem, name) {
    elem[name]();
  }

  window.onload = function () {
    test("function context ", function () {

      var Button = {
        click: function () {
          this.clicked = true;
        }
      };

      var elem = document.getElementById("test");
      elem.addEventListener("click", Button.click, false);
      trigger(elem, "click");

      assert(elem.clicked, " The clicked property was accidently set on the element");
      elem.removeEventListener("click", Button.click, false);
    });


  };

</script>

<ul id="results" />
</body>
</html>

 

 

please be NOTED that the unit.js is a home-crafted test framework, which I will share in some other post. 

 

 

as you can see, if you run this test, when you click on the button, the button with id test will have its clicked proeprty set, which is not what we want, it happens because the 'this' inside of the click function is referring to whatever context the function currently has.

 

a revised way is like this: 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../unit.js" type="text/javascript"></script>
    <script src="functioncontext.js" type="text/javascript"></script>
</head>
    <style type="text/css" >
      #results li.pass { color: Green }
      #results li.fail { color: Red }
    </style>

<body>
<button id="test">click me!</button>

<script type="text/javascript">

  function trigger(elem, name) {
    elem[name]();
  }

  window.onload = function () {

    test("function context 2", function () {

      var Button = {
        click: function () {
          this.clicked = true;
        }
      };

      var elem = document.getElementById("test");
      var eventhandler = bind(Button, "click");
      elem.addEventListener("click", eventhandler, false);
      trigger(elem, "click");
      assert(Button.clicked, "the clicked property was set on our object");
      elem.removeEventListener("click", eventhandler, false);
    });
  };

</script>

<ul id="results" />
</body>
</html>
 

 

the content of the "functioncontext.js" is like this 

 

 

function bind(context, name) {
  return function () {
    return context[name].apply(context, arguments);
  }
}
 

the key here is that we used the .call() funtion, which we can override the function context (affect what this binds to).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值