bar调用函数时"this"的行为令我感到困惑.请参阅下面的代码.当从单击处理程序调用bar而不是html元素时,有没有办法安排"this"成为一个普通的旧js对象实例?
// a class with a method
function foo() {
this.bar(); // when called here, "this" is the foo instance
var barf = this.bar;
barf(); // when called here, "this" is the global object
// when called from a click, "this" is the html element
$("#thing").after($("<div>click me</div>").click(barf));
}
foo.prototype.bar = function() {
alert(this);
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
您何时使用"this"关键字?
您好,我知道该This关键字用于引用该类的实例,但是,假设我有一个名为的类Life,它定义了两个字段,person(他们的名字)和他们的伙伴(他们的名字):
class Life
{
//Fields
private string _person;
private string _partner;
//Properties
public string Person
{
get { return _person; }
set { _person = value; }
}
public string Partner
{
get { return _partner; }
set { _partner = value; }
}
//Constructor 1
public Life()
{
_person = "Dave";
_partner = "Sarah";
MessageBox.Show("Life Constructor Called");
}
//Constructor 2
public Life()
{
this._person = "Dave";
this._partner = "Sarah"; …Run Code Online (Sandbox Code Playgroud) Goetz的Java Concurrency in Practice,第41页提到了this在构造过程中引用如何逃脱.一个"不要这样做"的例子:
public class ThisEscape {
public ThisEscape(EventSource source) {
source.registerListener(
new EventListener() {
public void onEvent(Event e) {
doSomething(e);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
这this是通过doSomething(e)引用封闭ThisEscape实例的事实"逃避" .可以通过使用静态工厂方法(首先构造普通对象,然后注册侦听器)而不是公共构造函数(完成所有工作)来解决这种情况.这本书继续:
从构造函数中发布对象可以发布未完全构造的对象.这是真实的,即使是公布在构造函数中的最后一条语句.如果
this参考在构造期间逃逸,则认为该对象未正确构造.
我不太懂.如果发布是构造函数中的最后一个语句,那么之前没有完成所有构建工作吗?怎么会是this由当时不是有效?显然有一些伏都教在那之后继续,但是什么?
在C++,在类构造函数中,我启动了一个新的线程,其中this指针作为参数,将广泛用于线程(例如,调用成员函数).这是件坏事吗?为什么以及后果是什么?
我的线程启动过程位于构造函数的末尾.
我试图了解这种名为JavaScript的黑色艺术 - 而且,我必须承认,对此非常兴奋.我一直在寻找代码示例,主要来自"easeljs",因为这是我将主要使用的.我有点困惑..
我(我想)理解使用prototypefor函数或属性作为class变量和this.someProp用于'实例'变量之间的区别(是的,我知道JavaScript中没有类.)
我看过的代码,我用作自己代码的模板,declare prototype变量然后用它来引用它们
在构造函数中:
this.name = name;
Run Code Online (Sandbox Code Playgroud)
然后声明:
Object.prototype.name;
Run Code Online (Sandbox Code Playgroud)
然后,
this.name = "Freddy";
Run Code Online (Sandbox Code Playgroud)
这是在使用'new'调用的函数内,所以在这种情况下,据我所知,它this指的是当前对象.令我困惑的是原型声明正在做什么以及为什么我们将它用于实例变量?
澄清:在下面的代码中,我没有看到radius的原型声明实现了什么:
(function(){
// constructor
function MyCircle(radius){
this.radius = radius;
}
MyCircle.prototype.radius;
this.area = function(){
return 3.14*this.radius*this.radius;
};
window.MyCircle = MyCircle;
}());
Run Code Online (Sandbox Code Playgroud) 我想知道forEach回调函数的'this'值(或调用上下文)是什么.此代码似乎不起作用:
var jow = [5, 10, 45, 67];
jow.forEach(function(v, i, a){
this[i] = v + 1;
});
alert(jow);
Run Code Online (Sandbox Code Playgroud)
谢谢你向我解释.
在Java中,您可以通过执行以下操作来引用当前对象:this.x = x.你是如何用C++做到的?
假设这些代码示例中的每一个都是被调用类的一部分Shape.
Java的:
public void setX(int x)
{
this.x = x;
}
Run Code Online (Sandbox Code Playgroud)
C++:
public:
void setX(int x)
{
//?
}
Run Code Online (Sandbox Code Playgroud) 在Scala中使用JDBC的示例中,有以下代码:
this.synchronized {
if (!driverLoaded) loadDriver()
}
Run Code Online (Sandbox Code Playgroud)
为什么this.synchronized而不仅仅是synchronized?
我的老师说,当我尝试访问方法中的实例变量时,我应该总是使用this关键字,否则我会执行双重搜索.本地范围搜索,然后是实例范围搜索.
例:
public class Test(){
int cont=0;
public void Method(){
System.out.println(cont);//Should I use This.cont instead?
}
}
Run Code Online (Sandbox Code Playgroud)
我希望他错了,但我找不到任何论据.
我发现了一个很难的方法,就是不能简单地将一个对象的函数传递给Bluebird then.我假设Bluebird then正在做一些魔术,并在匿名函数中包装传入的函数.所以我附加了一个.bind函数,它工作.这是用蓝鸟做这个的正确方法吗?还是有更好的方法吗?
var Promise = require("bluebird")
var Chair = function(){
this.color = "red"
return this
}
Chair.prototype.build = function(wood){
return this.color + " " + wood
}
var chair = new Chair()
//var x = chair.build("cherry")
Promise.resolve("cherry")
.then(chair.build.bind(chair)) // color is undefined without bind
.then(console.log)
Run Code Online (Sandbox Code Playgroud)
我知道这些都不是异步的,所以请使用同步示例,我的用法是异步.
this ×10
javascript ×4
java ×3
c++ ×2
constructor ×2
bluebird ×1
c# ×1
concurrency ×1
foreach ×1
function ×1
instance ×1
invocation ×1
jquery ×1
local ×1
node.js ×1
promise ×1
prototype ×1
publishing ×1
scala ×1
scope ×1
synchronized ×1