Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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和这个?_Javascript_This - Fatal编程技术网

Javascript和这个?

Javascript和这个?,javascript,this,Javascript,This,Javascript中的“this”对象让我感到不安 因此,可以定义“this”具有属于执行上下文的属性 当我们从特定上下文调用函数时,“this”将由被调用方定义 现在让我们假设我得到了以下代码: 函数f4(){ this.herp=“derp”; this.test='2'; } 函数事物(){ this.prop1=“无特殊”; this.test='1'; f4(); f4.呼叫(此); } var thing=新事物(); log('herp:'+thing.herp); log('

Javascript中的“this”对象让我感到不安

因此,可以定义“this”具有属于执行上下文的属性

当我们从特定上下文调用函数时,“this”将由被调用方定义

现在让我们假设我得到了以下代码:

函数f4(){
this.herp=“derp”;
this.test='2';
}
函数事物(){
this.prop1=“无特殊”;
this.test='1';
f4();
f4.呼叫(此);
}
var thing=新事物();
log('herp:'+thing.herp);

log('test:'+thing.test)1。如果使用new关键字调用函数,将指向新构造的对象

function fun() { this.a = 3; }

var obj = new fun();

console.log(obj.a); // 3
2。如果我们使用硬绑定(绑定函数),这将指向我们在绑定函数参数中传递的对象

function fun1() { this.a = 3; }

var obj1 = { a: 0 };

var fun2 = fun1.bind(obj1);

fun2();

console.log(obj1.a); // 3
现在,当我们调用fun2时,这个关键字将指向obj1,即使我们将fun2指定给另一个对象并使用点符号从它调用时也是如此

var obj2 = { a: 0, fun: fun2 };

obj2.fun();

console.log(obj2.a) // 0
但是,当我们使用fun2作为带有new关键字的构造函数时,在这种情况下,这个将指向新创建的对象,我们可以说new关键字比硬绑定更强

function fun1() { this.a = 3; }

var obj1 = { a: 0 };

var fun2 = fun1.bind(obj1)

var obj2 = new fun2()

console.log(obj1.a) // 0
console.log(obj2.a) // 3
3。如果我们使用调用或应用函数调用函数,这将指向我们在函数参数中传递的对象。

function fun() { this.a = 3; }

var obj = {a: 0};

fun.call(obj);

console.log(obj.a); // 3
4。如果我们使用点符号直接从对象调用函数,这将指向该对象

function fun() { this.a = 3; }

var obj = {a: 0, fun: fun};

obj.fun();

console.log(obj.a); // 3
5。在几乎所有其他情况下,这将指向全局对象

function fun() { this.a = 3; }

var obj = {a: 0, fun: fun};

obj.fun();

console.log(obj.a); // 3
现在让我们来看看你的例子

function f4(){

    this.herp = "derp";

    this.test = '2';

}



function Thing(){

    this.prop1="nothingSpecial";

    this.test = '1';

    f4();

    f4.call(this);

}



 var thing = new Thing();

 console.log('herp:'+thing.herp);

 console.log('test:'+thing.test);
当您使用Thing()作为带有new关键字的构造函数时,Thing函数中的该指向新创建的对象(为简单起见,假设该对象指向Thing,但实际上new operator创建了一个新对象,然后使用构造函数对其进行设置,最后将该对象指定给Thing)

作为一个指向thing对象,您在thing-prop1和test中创建了两个变量:

thing = {

    prop1: "nothingSpecial",

    test: "1"

}
然后当你第一次调用f4时,它里面的这个指向全局,不管f4被称为事物的内部函数。所以你设置了两个全局变量herp和test。此时并没有thing.herp变量,所以当您尝试注销它时,console会打印未定义的内容

thing = {

    prop1: "nothingSpecial",

    test: "1"

}

// global objects

console.log(test) // 2

console.log(herp) // "derp"
接下来,您使用一个调用函数调用f4,所以您将this
从Thing传递到f4,因为this在Thing point to Thing object中,this在f4中也指向Thing object,所以现在f4函数在Thing内部创建了新变量herp,并将测试从'1'覆盖到'2'

thing = {

    prop1: "nothingSpecial",

    test: "2",

    herp: "derp"


}

那么问题是什么呢?@hindmost为什么当我们第一次调用f4()时,它没有将上下文的this覆盖为第二个语句(f4.call(this))?抱歉,但是您需要阅读“您不知道js”上的对象部分,这将非常有帮助。JavaScrfipt这总是让人困惑,你对它的理解是错误的<代码>此
与执行上下文完全无关,除非您手动分配它。读一下