zoukankan      html  css  js  c++  java
  • AngularJS的this详解

    【this详解】
            
              1、谁最终调用函数,this指向谁。
                 ① this指向的,永远只可能是对象!!!!!!
                 ② this指向谁,永远不取决于this写在哪!!而是取决于函数在哪调用。
                 ③ this指向的对象,我们称之为函数的上下文context,也叫函数的调用者
             
              2、※※※※※this指向的规律(与函数调用的方式息息相关):
                 this指向的情况,取决于函数调用的方式有哪些:
                 ① 通过函数名()直接调用:this指向window
                 ② 通过对象.函数名()调用的:this指向这个对象
                 ③ 函数作为数组的一个元素,通过数组下标调用的:this指向这个数组
                 ④ 函数作为window内置函数的回调函数调用:this指向window
                  setInterval  setTimeout 等...
                 ⑤ 函数作为构造函数,用new关键字调用时:this指向新new出的对象

    function func(){
                console.log(this);
            }
            
            ① 通过函数名()直接调用:this指向window
            func(); this--->window
            
            ② 通过对象.函数名()调用的:this指向这个对象
                 狭义对象
                var obj = {
                    name:"obj",
                    func1 :func
                };
                obj.func1();  this--->obj
                
                 广义对象
                document.getElementById("div").onclick = function(){
                    this.style.backgroundColor = "red";
                };  this--->div
            
            ③ 函数作为数组的一个元素,通过数组下标调用的:this指向这个数组
            var arr = [func,1,2,3];
            arr[0]();   this--->arr
            
            ④ 函数作为window内置函数的回调函数调用:this指向window
            setTimeout(func,1000);// this--->window
            setInterval(func,1000);
            
            ⑤ 函数作为构造函数,用new关键字调用时:this指向新new出的对象
            var obj = new func(); //this--->new出的新obj
            
            
     

  • 相关阅读:
    python simplejson and json 使用及区别
    python 网页抓取并保存图片
    word2vec剖析,资料整理备存
    centos 安装LAMP环境后装phpmyadmin
    centos 安装卸载软件命令 & yum安装LAMP环境
    Ubuntu终端快捷键使用
    Ubuntu终端文件的压缩和解压缩命令
    文本预处理去除标点符号
    朴素贝叶斯分类器的应用
    Win32环境下代码注入与API钩子的实现(转)
  • 原文地址:https://www.cnblogs.com/dsmf/p/6854266.html
Copyright © 2011-2022 走看看