javascript之this

对应有道云笔记地址(有道云上排版更优):http://note.youdao.com/noteshare?id=f95bbbf96296236283b4963cd08d4fb6&sub=8F4C4439AE924535B8AD29FECB3C17D2

/**
* Created by: yel
* Date: 2018.01.19
* from: http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html
*/

this用法解析

1.this是javascript中的一个关键字,
2.this在函数执行时生成
3.this指的就是调用该函数的那个对象

eg:
  var obj = {
     age:11,
     say:function(){
            alert(this.age)
          }
    };
 obj.say(); // 11

 

注意:如果this没在任何方法中(或该方法不属于任何其它对象),则是指全局对象。

eg:
  function p(){
    function c(){
      console.log(this);
    }
    c();
  }
  p()==>window

posted @ 2018-10-09 17:08  鳯訡  阅读(137)  评论(0编辑  收藏  举报