zoukankan      html  css  js  c++  java
  • ?this&函数自身的引用

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
            var obj2 = {
                name:"ssss"
            }
            console.log(window.obj2);//Object {name: "sjr"}
        </script>
    </head>
    <body>
    <textarea id="output" cols="80" rows="10"></textarea>
    <script>
        (function(){
            var obj = {//局部变量,只能在function中被引用,并不会成为谁的属性
                name:"sjr"
            }
            console.log(this);//window(this指的是,调用函数的那个对象)
            console.log(this.obj);//undefined
        })()
    </script>
    </body>
    </html>

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions

    在函数执行时,this 关键字并不会指向正在运行的函数本身,而是指向调用该函数的对象.所以,如果你想在函数内部获取函数自身的引用,只能使用函数名或者使用arguments.callee属性(严格模式下不可用),如果该函数是一个匿名函数,则你只能使用后者.

    arguments.callee

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/arguments/callee

    描述

    callee 是 arguments 对象的属性。在该函数的函数体内,它可以指向当前正在执行的函数。当函数是匿名函数时,这是很有用的, 比如没有名字的函数表达式 (也被叫做"匿名函数")。

  • 相关阅读:
    July 08th. 2018, Week 28th. Sunday
    July 07th. 2018, Week 27th. Saturday
    兄弟组件bus传值
    vue 父子组件传值
    路由传值的三种方式
    jQuery 操作表格
    原生js实现开关功能
    跨域解决方法
    正则判断密码难度
    cookie封装函数
  • 原文地址:https://www.cnblogs.com/darr/p/4809873.html
Copyright © 2011-2022 走看看