我有这个方法,我想使用$ this,但我得到的是:致命错误:当不在对象上下文中时使用$ this.
我怎样才能让它发挥作用?
public static function userNameAvailibility()
{
$result = $this->getsomthin();
}
Run Code Online (Sandbox Code Playgroud) 使用$(this)与此相比有什么根本区别
$('.viewComments').click(function(ev){
//returns the desired value
alert(this.getAttribute('id'));
//Gives an error sayin function is not defined
alert($(this).getAttribute('id'));
//returns the desired value
alert($(this).attr('id'));
});
Run Code Online (Sandbox Code Playgroud)
我认为"$(this)"将包含"this"具有的所有功能以及更多..但似乎并非如此.
那么究竟是什么(这个)?和
我在使用它时知道哪些功能可用吗?(我知道我可以通过萤火虫来获取它们.但我想知道是否有其他方式 - 某些文档可能是)
显然,您可以this从结构中的任何位置更改值(但不能在类中):
struct Point
{
public Point(int x, int y)
{
this = new Point();
X = x; Y = y;
}
int X; int Y;
}
Run Code Online (Sandbox Code Playgroud)
我以前也没见过这种情况,也从未见过它.为什么一个人想要这样做?Eric Lippert提醒我们,必须证明某个功能是合理的.有什么好的用例可以证明这一点?有什么场景这是非常宝贵的吗?我找不到任何文件1.
此外,对于调用构造函数,已经有一个更好的已知替代语法,因此此功能有时是多余的:
public Point(int x, int y)
: this()
{
X = x; Y = y;
}
Run Code Online (Sandbox Code Playgroud)
我需要this从我的setInterval处理程序访问
prefs: null,
startup : function()
{
// init prefs
...
this.retrieve_rate();
this.intervalID = setInterval(this.retrieve_rate, this.INTERVAL);
},
retrieve_rate : function()
{
var ajax = null;
ajax = new XMLHttpRequest();
ajax.open('GET', 'http://xyz.com', true);
ajax.onload = function()
{
// access prefs here
}
}
Run Code Online (Sandbox Code Playgroud)
我如何访问this.prefs ajax.onload?
在C++中,关键字this通常是否省略?例如:
Person::Person(int age) {
_age = age;
}
Run Code Online (Sandbox Code Playgroud)
相反:
Person::Person(int age) {
this->_age = age;
}
Run Code Online (Sandbox Code Playgroud) 当我使用this关键字访问类中的非静态变量时,Java不会给出任何错误.但是当我不使用它时,Java会出错.我为什么要用this?
我知道通常应该什么时候使用this,但这个例子与正常用法非常不同.
例:
class Foo {
// int a = b; // gives error. why ?
int a = this.b; // no error. why ?
int b;
int c = b;
int var1 = this.var2; // very interesting
int var2 = this.var1; // very interesting
}
Run Code Online (Sandbox Code Playgroud) 我很感兴趣在JS中使用call()方法的原因是什么.它似乎重复了通常的调用方法this.
例如,我有一个call()代码.
var obj = {
objType: "Dog"
}
f = function(did_what, what) {
alert(this.objType + " " + did_what + " " + what);
}
f.call(obj, "ate", "food");
Run Code Online (Sandbox Code Playgroud)
输出是"狗吃食物".但是同样的结果我可以将函数分配给对象.
var obj = {
objType: "Dog"
}
f = function(did_what, what) {
alert(this.objType + " " + did_what + " " + what);
}
obj.a = f;
obj.a("ate", "food");
Run Code Online (Sandbox Code Playgroud)
结果是一样的.但这种方式更容易理解和使用.为什么需要call()?
我正在尝试从通过请求传入的宽度计算比例高度(同时排除静态高度元素)(默认为560).
但是,wF.h评估为NaN.如果我this.w用560 替换它可以工作,但在尝试引用w属性时则不行wF.
var wF = {
w : 560,
h : (312 - 42) / (560 / this.w) + 42
};
Run Code Online (Sandbox Code Playgroud)
是什么赋予了?
我拒绝连续使用两个普通的变量,因为我试图从JS中获得很好的代码.
更新:
感谢所有帮助解释和解决我的问题的人.我想我只需要习惯它.我将分阶段设置对象以继续项目,即使它仍然让我稍微烦恼;).对于遇到类似问题的人,我发现并阅读了一篇关于这个主题的好文章:http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/
我的问题是:
function Foo()
{
this.foo = "bar"; // <- What is "this" here?
}
Run Code Online (Sandbox Code Playgroud)
从我可以告诉它取决于如何Foo使用,即作为构造函数或函数.什么可以this在不同的情况下?
我早就做了一点Python.然而,我现在转向Java.我想知道Python"self"方法和Java"this"之间是否存在任何差异.
我知道"自我"不是关键字,而"这"是关键字.这几乎是我能想到的.我错过了什么吗?