令人困惑的JavaScript语句:"var x = new this();"

Ada*_*sev 16 javascript inheritance class

我以为我理解了JavaScript原型对象的概念,以及[[proto]],直到我看到一些关于类继承的帖子.

首先,"JavaScript OOP - 智能方式",网址http://amix.dk/blog/viewEntry/19038

请参阅实施部分:

var parent = new this('no_init');
Run Code Online (Sandbox Code Playgroud)

还有John Resig的伟大博客上的"简单JavaScript继承".

var prototype = new this();
Run Code Online (Sandbox Code Playgroud)

究竟new this();是什么意思?

这句话对我没有意义,因为我的理解是this指向一个对象而不是构造函数.我也尝试在Firebug中测试语句来解决这个问题,我收到的只是语法错误.

我的头已经完全旋转了.

有人可以详细解释一下吗?

Abe*_*efe 7

在 javascriptstatic函数中,您可以new this()像这样调用,

var Class = function(){}; // constructor
Class.foo = function(){return this;} // will return the Class function which is also an object
Run Code Online (Sandbox Code Playgroud)

所以,

Class.foo = function(){ return new this();} // Will invoke the global Class func as a constructor
Run Code Online (Sandbox Code Playgroud)

这样你就得到了一个静态工厂方法。这个故事的寓意是,当你不调用函数时,不要忘记函数就像任何其他对象一样。


Dom*_*cio 6

令人困惑的是,我认为,这就是"这个"真正来自的地方.所以忍受我 - 这是一个非常简短的解释,我希望能够说得很清楚.

在JavaScript中,函数内部"this"的含义总是在调用函数时确定.当你这样做时:

jimmy.nap();

小睡功能(方法)运行并接收吉米作为"这个".

什么对象提到午睡是无关紧要的.例如:

var jimmy = {}, billy = {};
jimmy.nap = function(){ alert("zzz"); };
var jimmy_nap = jimmy.nap;
jimmy_nap(); // during this function's execution, this is *NOT* jimmy!
             // it is the global object ("window" in browsers), which is given as the 
             // context ("this") to all functions which are not given another context.
billy.sleep = jimmy.nap;
billy.sleep(); // during this function's excution, this is billy, *NOT* jimmy
jimmy.nap(); //okay, this time,  this is jimmy!
Run Code Online (Sandbox Code Playgroud)

换句话说,只要你有:

var some_func = function(arg1, arg2){ /*....*/ };
// let's say obj and other_obj are some objects that came from somewhere or another
obj.some_meth = some_func;
other_obj.some_meth = some_func;
obj.some_meth(2, 3);
other_obj.some_meth(2, 3);
Run Code Online (Sandbox Code Playgroud)

什么被"翻译"成(不是字面意思 - 这是教学法,而不是关于javascript解释器实际上是如何工作的)是这样的:

var some_func = function(this, arg1, arg2){ /* ...*/ };
// let's say obj and other_obj are some objects that came from somewhere or another
obj.some_meth = some_func;
other_obj.some_meth = some_func;
obj.some_meth(obj, 2, 3);
other_obj.some_meth(other_obj, 2, 3);
Run Code Online (Sandbox Code Playgroud)

因此,请注意该页面上的示例中如何使用extend:

UniversityPerson = Person.extend({ /* ... */ });
Run Code Online (Sandbox Code Playgroud)

流行测验:当延伸运行时,它认为"这个"是指什么?答:没错."人".

所以上面令人费解的代码实际上与(在特定情况下)相同:

var prototype = new Person('no_init');
Run Code Online (Sandbox Code Playgroud)

不再那么神秘了,是吗?这是可能的,因为与某些语言不同,JavaScript变量(包括"this")可以包含任何值,包括Person等函数.

(没有任何东西可以使Person特别是构造函数.任何函数都可以使用new关键字调用.如果我回想起确切的语义,我认为它们是当使用new关键字调用函数时,会自动给出一个空对象({})作为它的上下文("this")并且当函数返回时,返回值是同一个对象,除非(可能?)函数返回其他内容)

这是一个很酷的问题,因为它说明了JavaScript的整洁或奇怪的一个非常重要的部分(取决于你如何看待它).

这是否回答你的问题?如有必要,我可以澄清一下.


str*_*ger 5

AJS.Class 有效*翻译这个:

var Person = new AJS.Class({
    init: function(name) {
        this.name = name;
        Person.count++;
    },
    getName: function() {
        return this.name;
    }
});
Person.count = 0;
Run Code Online (Sandbox Code Playgroud)

进入这个:

var Person = function (name) {
    this.name = name;
    Person.count++;
};

Person.prototype = {
    getName: function() {
        return this.name;
    }
};

Person.extend = AJS.Class.prototype.extend;
Person.implement = AJS.Class.prototype.implement;

Person.count = 0;
Run Code Online (Sandbox Code Playgroud)

因此,在这种情况下,thisAJS.Class.prototype.extendPerson,这是因为:

Person.extend(...);
// is the same as
Person.extend.call(Person, ...);
// is the same as
AJS.Class.prototype.extend.call(Person, ...);
Run Code Online (Sandbox Code Playgroud)

*有很多案例我不会过去; 这个重写是为了简化理解问题.