我注意到,似乎没有明确解释this
关键字是什么以及如何在Stack Overflow站点上的JavaScript中正确(和错误地)使用它.
我亲眼目睹了一些非常奇怪的行为,并且无法理解为什么会发生这种行为.
this
工作如何以及何时使用?
要使用公共方法创建JavaScript类,我会执行以下操作:
function Restaurant() {}
Restaurant.prototype.buy_food = function(){
// something here
}
Restaurant.prototype.use_restroom = function(){
// something here
}
Run Code Online (Sandbox Code Playgroud)
这样我班级的用户可以:
var restaurant = new Restaurant();
restaurant.buy_food();
restaurant.use_restroom();
Run Code Online (Sandbox Code Playgroud)
如何创建一个可由buy_food
和use_restroom
方法调用的私有方法,但不能由类的用户创建外部方法?
换句话说,我希望我的方法实现能够:
Restaurant.prototype.use_restroom = function() {
this.private_stuff();
}
Run Code Online (Sandbox Code Playgroud)
但这不应该奏效:
var r = new Restaurant();
r.private_stuff();
Run Code Online (Sandbox Code Playgroud)
我如何定义private_stuff
为私有方法,所以这两个都适用?
我已经阅读了Doug Crockford的几次写法,但看起来似乎不能通过公共方法调用"私有"方法,并且可以在外部调用"特权"方法.
我在WebKit HTML 5 SQL存储笔记演示的源代码中看到以下内容:
function Note() {
var self = this;
var note = document.createElement('div');
note.className = 'note';
note.addEventListener('mousedown', function(e) { return self.onMouseDown(e) }, false);
note.addEventListener('click', function() { return self.onNoteClick() }, false);
this.note = note;
// ...
}
Run Code Online (Sandbox Code Playgroud)
笔者采用自我在一些地方(函数体)及本在其他地方(的函数方法的参数列表中定义的机构).这是怎么回事?现在我已经注意到了它,我会在各处开始看到它吗?
使用实例方法作为事件处理程序的回调改变的范围this
从"我的实例"到"无论只是调用的回调".所以我的代码看起来像这样
function MyObject() {
this.doSomething = function() {
...
}
var self = this
$('#foobar').bind('click', function(){
self.doSomethng()
// this.doSomething() would not work here
})
}
Run Code Online (Sandbox Code Playgroud)
它有效,但这是最好的方法吗?这对我来说很奇怪.
我将使用两个外部脚本作为支付网关.现在两者都放在'index.html'文件中.但是,我不想在开头加载这些文件.仅当用户打开特定组件(使用路由器视图)时才需要支付网关.反正有没有实现这个目标?
我有很少的javascript广泛使用关键字"那".我看到很多帖子都在谈论javascript关键字"this".
我想在javascript上下文中理解这个关键词的含义,以及它的可见性/范围.
就像是
that.someFunctionaName(someParameter)
Run Code Online (Sandbox Code Playgroud)
这是什么意思?
我理解关键字"this"始终指向当前对象的所有者.
嗨,我是新的JavaScript
使用这条线有什么好处
var that = this
一个例子
function Person( firstname, lastname, age ) {
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
getfullname = function() {
return firstname + “ “ + lastname;
};
var that = this;
this.sayHi = function() {
document.write( “Hi my name is “ + getfullname() + “ and I am “ + that.age + “years old.”);
};
}
Run Code Online (Sandbox Code Playgroud)
谢谢
这是一个带有公共和私有方法的简单Javascript类的示例(小提琴:http://jsfiddle.net/gY4mh/).
function Example() {
function privateFunction() {
// "this" is window when called.
console.log(this);
}
this.publicFunction = function() {
privateFunction();
}
}
ex = new Example;
ex.publicFunction();
Run Code Online (Sandbox Code Playgroud)
从公共部门调用私有函数会导致"this"成为窗口对象.我应该如何确保使用类上下文而不是窗口调用我的私有方法?这会不合适吗?
我有问题仍然困扰我js oop - 我确定我做得不好,但我不能得到如何正确做到这一点.
例如,我有这个代码
Auth.prototype.auth = function () {
var request = new XMLHttpRequest();
request.open('GET', this.getAuthServerURL() + '/token', true);
request.send();
request.onloadend = function () {
var response = JSON.parse(request.responseText);
console.log(response);
if(response.result == 'found') {
var token = response.token;
this.setToken(token);
this.isSigned = true;
} else {
console.log('Not logged yet.');
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法从"request.onloadend"函数的上下文访问函数setToken - 这可能是因为我丢失了对"this"的引用.
这是什么问题的解决方案?我可以以某种方式将"this"var传递给此函数的上下文吗?
谢谢!
考虑以下 Javascript 类:
function ClassA() {
this.someVariable = someValue;
this.myHandler = function(){
// I WANT TO CALL InnerFunction
this.innerFunction();
};
this.innerFunction = function(){
// do something that accesses/manipulates someVariable
};
this.hookUp = function(id){
document.getElementById(id).onclick = this.myHandler;
};
};
...
...
var button = document.createElement('button');
button.setAttribute('id','mybuttonid');
// append button to the document
...
...
var x = new ClassA();
x.hookUp('mybuttonid');
Run Code Online (Sandbox Code Playgroud)
当我单击按钮时,处理程序会执行,但是,“this”现在指的是按钮元素而不是 ClassA 对象,因此它无法解析 innerFunction()。
我需要的是一种方法来向处理程序表明 this 的上下文是 ClassA 的实例(类似于 $.ajax({context: this....}),您可以在 . done() 或 .error() 处理程序),或一种将实例的引用传递给处理程序而不使处理程序在实例化时执行的方法。例如,如果我尝试将 'this' 作为参数传递给 myHandler (myHandler=function(ref){},然后更改:document.getElementById(id).onclick = …