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

  • 相关阅读:
    html5之缩放图标
    html5之图片的缩放scale
    html5之打地鼠100%胜率
    html5之调整旋转中心点
    html5之三角旋转
    html5中模块居中
    html5中2d图片旋转
    html5之动态移动图
    html5之steps
    读微信开放文档未解记录
  • 原文地址:https://www.cnblogs.com/darr/p/4809873.html
Copyright © 2011-2022 走看看