zoukankan      html  css  js  c++  java
  • this的使用

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>this的使用</title>
    </head>
    <body>
    <script>
    //如果嵌套函数作为方法调用,其this的值指向调用它的对象。如果嵌套函数作为函数调用,其this值不是全局对象(非严格模式)就是undefined(严格模式下)。如果想访问外部的this值,需要将this的值保存在一个变量里,这个变量和内部函数都在同一个作用域内。通常使用变量self来保存this。
    var o = {
    m:function(){
    var self = this;//将this的值保存至一个变量中
    console.log(self === o);//true,this就是o这个对象
    f();//调用辅助函数f()
    function f(){
    console.log(this === o);//false;this的值是全局对象或者undefined
    console.log(self === o);//true;self指外部函数的this值
    }
    }
    }
    o.m();
    </script>
    </body>
    </html>

  • 相关阅读:
    MongoDB入门
    MongoDB基础命令
    MongoDB查询
    MongoDB索引
    MongoDB聚合
    MongoDB进阶
    Elasticsearch简介与安装
    ElasticSearch索引
    shiro xml标准配置
    shiro双realm验证
  • 原文地址:https://www.cnblogs.com/studyh5/p/9316651.html
Copyright © 2011-2022 走看看