Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对嵌套函数中的父函数使用“this”_Javascript_Jquery_Class_Oop_Object - Fatal编程技术网

Javascript 对嵌套函数中的父函数使用“this”

Javascript 对嵌套函数中的父函数使用“this”,javascript,jquery,class,oop,object,Javascript,Jquery,Class,Oop,Object,在Apple“classs”中,嵌套函数countSeeds()如何检索值this.price jsfiddle: 输出 17 undefined 将var self=this放在Apple的顶部,然后在嵌套函数中将其称为self i、 e: 您还可以将self=this语句放在this.cutOpen的开头,因为它仍然引用cutOpen中的Apple对象(因为它是Apple的一种方法) 更新 大多数evergreen浏览器现在都支持箭头函数,因此您可以像这样编写: Apple = funct

在Apple“classs”中,嵌套函数
countSeeds()
如何检索值
this.price

jsfiddle:

输出

17
undefined

var self=this
放在Apple的顶部,然后在嵌套函数中将其称为self

i、 e:

您还可以将
self=this
语句放在this.cutOpen的开头,因为它仍然引用cutOpen中的Apple对象(因为它是Apple的一种方法)

更新

大多数evergreen浏览器现在都支持箭头函数,因此您可以像这样编写:

Apple = function() {
    var self = this;
    this.price = 17

    this.cutOpen = function() {

        console.log(this.price);
        let countSeeds = () => {
            console.log(this.price);
        };
        countSeeds();
    }        
}

这在IE11或其他较旧的浏览器中不起作用,除非您使用某种transpiler以较旧的javascript为目标。

默认情况下,当您在不提供上下文的情况下调用函数时,
指的是
窗口
对象。如果不喜欢默认设置,请尝试或设置上下文:

this.cutOpen = function() {

        console.log(this.price);
        countSeeds.call(this); //use call to set the context for the function.

        function countSeeds() {
            console.log(this.price);
        }
    }  
我建议您将
countSeeds
移动到构造函数之外,以防止它被多次重新定义:

function countSeeds() {
            console.log(this.price);
        }

Apple = function() {
    this.price = 17

    this.cutOpen = function() {

        console.log(this.price);
        countSeeds.call(this);
    }        
}
或者将
countSeeds
定义为原型功能:

 Apple = function() {
        this.price = 17

        this.cutOpen = function() {

            console.log(this.price);
            this.countSeeds(); //use countSeeds as an instance method.
        }        
    }

    Apple.prototype.countSeeds = function () {
          console.log(this.price);
    }

在我看来,
countSeeds
在这种情况下应该是一个实例方法,因为它总是访问当前实例的
price
属性,如果
这个
窗口

没有
countSeeds()
更改本身的
this
变量?函数上下文中的this对象设置为以下三个变量之一:1。如果函数是用new(newapple)调用的,那么它指的是构造函数将返回的对象。2.如果使用点语法(apple.cutOpen())或call或apply方法对对象调用函数,则该函数将引用该对象。3.如果直接调用该函数(countSeeds()),那么它将引用全局对象,在浏览器中它是窗口。您忘记了
.call()
.apply()
.bind()
,括号语法相当于点符号和箭头函数(即将标准化)。需要记住的重要一点是,执行上下文的
这个
绑定是通过调用函数的方式定义的。我要做的一个更改是,一旦您开始使用
self
,就要使用它来避免代码中的任何混乱。因此,函数中的第二行是
self.price=17self.cutOpen=function(){
@FabrícioMatté我没有忘记.call()或.apply(),我在点语法之后就提到了它们。不过我没有提到bind,我也没有提到arrow语法,因为它还没有得到广泛的实现(尽管当它出现时,这将是这个问题的最佳答案).好电话。
function countSeeds() {
            console.log(this.price);
        }

Apple = function() {
    this.price = 17

    this.cutOpen = function() {

        console.log(this.price);
        countSeeds.call(this);
    }        
}
 Apple = function() {
        this.price = 17

        this.cutOpen = function() {

            console.log(this.price);
            this.countSeeds(); //use countSeeds as an instance method.
        }        
    }

    Apple.prototype.countSeeds = function () {
          console.log(this.price);
    }