我刚刚开始使用原型JavaScript,并且我很难弄清楚this当范围发生变化时如何在原型函数内保留对主对象的引用.让我说明一下我的意思(我在这里使用jQuery):
MyClass = function() {
this.element = $('#element');
this.myValue = 'something';
// some more code
}
MyClass.prototype.myfunc = function() {
// at this point, "this" refers to the instance of MyClass
this.element.click(function() {
// at this point, "this" refers to the DOM element
// but what if I want to access the original "this.myValue"?
});
}
new MyClass();
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过以下方式执行此操作来保留对主对象的引用myfunc:
var myThis = this;
Run Code Online (Sandbox Code Playgroud)
然后使用myThis.myValue访问主对象的属性.但是当我有一大堆原型函数时会发生什么MyClass?我是否必须this在每个开头保存引用?似乎应该有一个更清洁的方式.这样的情况怎么样:
MyClass = function() {
this.elements $('.elements'); …Run Code Online (Sandbox Code Playgroud) 我有一个javascript类,每个方法都返回一个Qpromise.我想知道为什么this未定义method2和method3.有没有更正确的方法来编写此代码?
function MyClass(opts){
this.options = opts;
return this.method1()
.then(this.method2)
.then(this.method3);
}
MyClass.prototype.method1 = function(){
// ...q stuff...
console.log(this.options); // logs "opts" object
return deferred.promise;
};
MyClass.prototype.method2 = function(method1resolve){
// ...q stuff...
console.log(this); // logs undefined
return deferred.promise;
};
MyClass.prototype.method3 = function(method2resolve){
// ...q stuff...
console.log(this); // logs undefined
return deferred.promise;
};
Run Code Online (Sandbox Code Playgroud)
我可以通过使用bind:
function MyClass(opts){
this.options = opts;
return this.method1()
.then(this.method2.bind(this))
.then(this.method3.bind(this));
}
Run Code Online (Sandbox Code Playgroud)
但不完全确定为什么bind有必要; 正在.then()杀戮this?
我有一个关于如何在嵌套函数场景中处理"this"指针的问题.
假设我将以下示例代码插入到网页中.当我调用嵌套函数"doSomeEffects()"时出错.我检查了Firebug,它表明当我在嵌套函数中时,"this"指针实际上指向全局"窗口"对象 - 我没想到.我一定不能理解正确的东西,因为我认为既然我在对象的函数中声明了嵌套函数,它应该具有与函数相关的"局部"范围(即"this"指针将指向对象本身就像它是如何在我的第一个"如果"声明中.
任何指针(没有双关语意)将不胜感激.
var std_obj = {
options : { rows: 0, cols: 0 },
activeEffect : "none",
displayMe : function() {
// the 'this' pointer is referring to the std_obj
if (this.activeEffect=="fade") { }
var doSomeEffects = function() {
// the 'this' pointer is referring to the window obj, why?
if (this.activeEffect=="fade") { }
}
doSomeEffects();
}
};
std_obj.displayMe();
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试学习如何使用智能指针.然而,在做一些实验时,我发现了以下情况,我无法找到一个令人满意的解决方案:
想象一下,你有一个A类的对象是B类对象(孩子)的父对象,但两者都应该互相认识:
class A;
class B;
class A
{
public:
void addChild(std::shared_ptr<B> child)
{
children->push_back(child);
// How to do pass the pointer correctly?
// child->setParent(this); // wrong
// ^^^^
}
private:
std::list<std::shared_ptr<B>> children;
};
class B
{
public:
setParent(std::shared_ptr<A> parent)
{
this->parent = parent;
};
private:
std::shared_ptr<A> parent;
};
Run Code Online (Sandbox Code Playgroud)
问题是A类的一个对象如何std::shared_ptr将自身(this)传递给它的孩子?
有Boost共享指针(Get a boost::shared_ptrforthis)的解决方案,但是如何使用std::智能指针处理这个问题?
我想避免(大多数)Netbeans 6.9.1的警告,我的'Leaking this in constructor'警告有问题.
我理解这个问题,在构造函数中调用一个方法并传递" this"是危险的,因为" this"可能没有完全初始化.
很容易在我的单例类中修复警告,因为构造函数是私有的,只能从同一个类中调用.
旧代码(简化):
private Singleton() {
...
addWindowFocusListener(this);
}
public static Singleton getInstance() {
...
instance = new Singleton();
...
}
Run Code Online (Sandbox Code Playgroud)
新代码(简化):
private Singleton() {
...
}
public static Singleton getInstance() {
...
instance = new Singleton();
addWindowFocusListener( instance );
...
}
Run Code Online (Sandbox Code Playgroud)
如果构造函数是公共的并且可以从其他类调用,则此修复不起作用.如何修复以下代码:
public class MyClass {
...
List<MyClass> instances = new ArrayList<MyClass>();
...
public MyClass() {
...
instances.add(this);
}
}
Run Code Online (Sandbox Code Playgroud)
当然我想要一个修复,它不需要使用这个类修改我的所有代码(例如通过调用init方法).
我知道这可能是基本的痛苦,但我很难绕过它.
class Main
{
constructor()
{
requestAnimationFrame(this.update); //fine
}
update(): void
{
requestAnimationFrame(this.update); //error, because this is window
}
}
Run Code Online (Sandbox Code Playgroud)
似乎我需要一个代理,所以让我们说使用Jquery
class Main
{
constructor()
{
this.updateProxy = $.proxy(this.update, this);
requestAnimationFrame(this.updateProxy); //fine
}
updateProxy: () => void
update(): void
{
requestAnimationFrame(this.updateProxy); //fine
}
}
Run Code Online (Sandbox Code Playgroud)
但是来自Actionscript 3的背景,我不确定这里发生了什么.抱歉,我不确定Javascript的开始位置和TypeScript结束.
updateProxy: () => void
Run Code Online (Sandbox Code Playgroud)
而且,我不相信我这样做是对的.我想要的最后一件事是我的班级大部分都有aa()函数需要访问,aProxy()因为我觉得我写两次相同的东西?这是正常的吗?
我使用jQuery创建了一个'control',并使用jQuery.extend来帮助尽可能地将其作为OO.
在我的控件初始化期间,我连接各种点击事件,如此
jQuery('#available input',
this.controlDiv).bind('click', this, this.availableCategoryClick);
Run Code Online (Sandbox Code Playgroud)
请注意,我将'this'作为bind方法中的data参数.我这样做是为了让我可以获取附加到控件实例的数据而不是触发click事件的元素.
这很好用,但我怀疑有更好的方法
在过去使用过Prototype之后,我记得一个绑定语法,它允许你控制事件中'this'的值.
什么是jQuery方式?
我试过这个:
// mod.js
var a = 1;
this.b = 2;
exports.c = 3;
// test.js
var mod = require('./mod.js');
console.log(mod.a); // undefined
console.log(mod.b); // 2
console.log(mod.c); // 3, so this === exports?
Run Code Online (Sandbox Code Playgroud)
所以我想象require()可能是这样实现的:
var require = function (file) {
var exports = {};
var run = function (file) {
// include "file" here and run
};
run.apply(exports, [file]);
return exports;
}
Run Code Online (Sandbox Code Playgroud)
是对的吗?请帮我理解require(),或者在哪里可以找到源代码.谢谢!
this在Java中使用关键字的最佳做法是什么?例如,我有以下类:
class Foo {
Bar bar;
public Foo(Bar bar) {
this.bar = bar;
}
}
Run Code Online (Sandbox Code Playgroud)
这很好,但是如果我将构造函数中的语句更改为,Java就足够聪明地知道发生了什么
bar = bar;
Run Code Online (Sandbox Code Playgroud)
那么为什么要使用this关键字?(我知道在某些情况下,使用它是完全必要的,我只是要求这样的情况).实际上,我倾向于纯粹为了可读性而使用关键字,但通常的做法是什么?例如,在整个商店中使用它会使我的代码看起来有些混乱
boolean baz;
int someIndex = 5;
this.baz = this.bar.getSomeNumber() == this.someBarArray[this.someIndex].getSomeNumber();
Run Code Online (Sandbox Code Playgroud)
显然是一个糟糕的代码,但它说明了我的例子.在这些情况下,这仅仅取决于个人偏好吗?
this ×10
javascript ×4
c++ ×2
java ×2
jquery ×2
node.js ×2
scope ×2
apply ×1
coding-style ×1
constructor ×1
function ×1
nested ×1
netbeans ×1
netbeans-6.9 ×1
oop ×1
promise ×1
q ×1
require ×1
shared-ptr ×1
this-pointer ×1
typescript ×1