我注意到,似乎没有明确解释this
关键字是什么以及如何在Stack Overflow站点上的JavaScript中正确(和错误地)使用它.
我亲眼目睹了一些非常奇怪的行为,并且无法理解为什么会发生这种行为.
this
工作如何以及何时使用?
有没有办法在JavaScript中使用以下内容?
var foo = {
a: 5,
b: 6,
c: this.a + this.b // Doesn't work
};
Run Code Online (Sandbox Code Playgroud)
在当前形式中,此代码显然会抛出引用错误,因为this
没有引用foo
.但是,是有什么办法对早些时候宣布的其他属性在对象文本的属性值依赖?
我刚刚在JavaScript中遇到了一个有趣的情况.我有一个类,其方法使用object-literal表示法定义多个对象.在这些对象中,this
正在使用指针.从程序的行为,我推断出this
指针指的是调用方法的类,而不是文字创建的对象.
这似乎是随意的,尽管这是我期望它工作的方式.这是定义的行为吗?跨浏览器安全吗?有没有任何理由可以解释为什么它超出"规范如此说明"的方式(例如,它是否是一些更广泛的设计决策/哲学的结果)?简化代码示例:
// inside class definition, itself an object literal, we have this function:
onRender: function() {
this.menuItems = this.menuItems.concat([
{
text: 'Group by Module',
rptletdiv: this
},
{
text: 'Group by Status',
rptletdiv: this
}]);
// etc
}
Run Code Online (Sandbox Code Playgroud) 在ES6中,这两个都是合法的:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
Run Code Online (Sandbox Code Playgroud)
并且,作为速记:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用新的箭头功能?在尝试类似的东西
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
Run Code Online (Sandbox Code Playgroud)
要么
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息,提示该方法无权访问this
.这只是一个语法问题,还是你不能在ES6对象中使用fat-pipe方法?
我想知道点抽象方法(例如dog.bark
)是在运行时还是在编译时绑定.我的问题涉及以下代码,它会引发错误:
(true ? ''.toLowerCase : ''.toUpperCase)()
Run Code Online (Sandbox Code Playgroud)
但以下不是:
true ? ''.toLowerCase() : ''.toUpperCase()
Run Code Online (Sandbox Code Playgroud)
为什么我的字符串文字''
在第一个例子中没有得到解决?
我想了解ECMAScript 6中的箭头功能.
这是我在阅读时遇到的定义:
箭头函数具有隐式
this
绑定,这意味着this
箭头函数内部值的值与this
定义箭头函数的范围中的值相同!
根据定义,我相信this
一个arrow function
应该包含箭头函数定义的相同块级别值.
码:
var test = {
id: "123123",
k: {
laptop: "ramen",
testfunc: () => console.log(this)
}
}
console.log(test.k.testfunc);
Run Code Online (Sandbox Code Playgroud)
但是,我从代码中得到了这个结果
function testfunc() {
return console.log(undefined);
}
Run Code Online (Sandbox Code Playgroud)
我以为我会得到的输出是:
{"laptop": "ramen"}
Run Code Online (Sandbox Code Playgroud)
如果我跑了
console.log(test.k.testfunc());
我正在学习javascript,我遇到了一个疑问.为什么在第一个示例中未定义"this"的值,但在第二个示例中正确打印出来.
例1:
var myNamespace = {
myObject: {
sayHello: function() {
console.log( "name is " + this.myName );
},
myName: "john"
}
};
var hello = myNamespace.myObject.sayHello;
hello(); // "name is undefined"
Run Code Online (Sandbox Code Playgroud)
例2:
var myNamespace = {
myObject: {
sayHello: function() {
console.log( "Hi! My name is " + this.myName );
},
myName: "Rebecca"
}
};
var obj = myNamespace.myObject;
obj.sayHello();//"Hi! My name is Rebecca"
Run Code Online (Sandbox Code Playgroud)
为什么"this"的值在函数内发生变化.我错过了什么概念?
这是代码:
for (var i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i); //prints 9 10 times
console.log(this.i); //prints 0, 1, 2...9
}.bind({i:i}), i * 1000);
}
Run Code Online (Sandbox Code Playgroud)
为什么i
以及this.i
是指不同的东西?
将此与在全局范围内执行的一些代码进行对比:
var x = 5;
console.log(x);
console.log(this.x);//both will print 5
Run Code Online (Sandbox Code Playgroud)
这里的范围是全球性的,背景也是如此.变量声明在全局上下文中设置相同名称的属性.另一方面,在函数范围内,这不会发生.
var a = function() {
var x = 5;
console.log(x); //5
console.log(this.x); //undefined
console.log(i); //undefined
console.log(this.i); //10
}.bind({i: 10});
a();
Run Code Online (Sandbox Code Playgroud)
即使我们将全局上下文传递到本地范围,在函数中声明变量也不会将其设置为全局上下文的属性.
var a = function() {
var x = 5;
console.log(x); //5
console.log(this.x); //undefined
}.bind(window);
a(); …
Run Code Online (Sandbox Code Playgroud) 有人可以解释一下为什么'this'在下面指向DOM对象而不是Window?
$("a").click(function() {
console.log(this);
});
Run Code Online (Sandbox Code Playgroud)
这导致:
<a id="first" href="http://jquery.com">
Run Code Online (Sandbox Code Playgroud)
考虑以下应该是相同的场景:
function Foo() {
this.click = function(f) {
f();
}
}
var obj = new Foo();
obj.click(function() {
console.log(this);
});
Run Code Online (Sandbox Code Playgroud)
我们得到的是Window Object(我所期待的).
我尝试了两种引用函数的方法:
a=function(){
somefunction();
}
Run Code Online (Sandbox Code Playgroud)
a=somefunction;
Run Code Online (Sandbox Code Playgroud)
凡somefunction
在这两种情况下的情况如下:
somefunction :function() {
alert("hello");
}
Run Code Online (Sandbox Code Playgroud)
这两种方式有什么区别吗?