简体   繁体   English

从这里访问原型功能

[英]access prototype function from this

I am trying to fix some Javascript that uses functions and prototype functions. 我正在尝试修复一些使用函数和原型函数的Javascript。 For some reason the prototype function is always undefined when I try to access it and I can't figure out why. 由于某种原因,当我尝试访问原型函数时, 总是无法定义原型函数,因此我无法弄清原因。

Here is a simple example of what I'm trying to do. 这是我正在尝试做的一个简单示例。 Basically, I want to reference the _open prototype from within the original Container function declaration using this . 基本上,我想使用this从原始Container函数声明中引用_open原型。

Container();

function Container() {
    alert(this._open);
}

Container.prototype._open = function() {
    alert("hey");
}

You can see in fiddle that it just alerts "undefined." 您会发现 ,它只是在警告“未定义”。 But this question and this question both show examples of people doing this. 但是这个问题这个问题都显示了人们这样做的例子。 Why do I keep getting undefined? 为什么我总是变得不确定?

Three things: 三件事:

  • use new Container(); 使用new Container(); instead of Container(); 代替Container(); .
  • Move this new Container(); 移动这个new Container(); line AFTER all prototype additions. 所有prototype增加之后的生产线。
  • Call this._open(); 调用this._open(); instead of alert(this._open); 而不是alert(this._open); to actually execute the function. 实际执行功能。

So your code should look like this: 因此,您的代码应如下所示:

function Container() {
    this._open();
}   
Container.prototype._open = function() {
    alert('open');
}
new Container();

Hope this helps. 希望这可以帮助。

function Container() {
    this._open();
}

Container.prototype._open = function() {
    alert("hey");
}

var container = new Container();

Try the above. 尝试以上。 You need to create an instance of the object using new . 您需要使用new创建对象的实例。 Otherwise this refers to the global object and not the prototype members. 否则, this引用的是全局对象,而不是原型成员。

Using constructors without new() causes weird bugs. 使用没有new()的构造函数会导致奇怪的错误。 Since this will refer to the global objected === window. 因为this将引用全局对象===窗口。

Call Container() after the definition and with the new evaluator : var instace = new Container(); 在定义之后并使用new评估程序调用Container()var instace = new Container();

Example

 function Container() { this._open(); } Container.prototype._open = function(e) { alert("hey"); } var instance = new Container(); 

And maybe you don't need two alerts. 也许您不需要两个警报。

Your code takes advantage (perhaps unintentionally) of hoisting. 您的代码利用了提升(可能是无意间)的优势。 So it looks like the prototype is built prior to execution but in fact it was not. 因此, 看起来原型是在执行之前构建的,但实际上并非如此。

This is what your code actually looks like 这就是您的代码实际的样子

function Container(){
    alert(this._open);
}//functions are "hoisted"

Container();

Container.prototype._open = function() {
    alert("hey");
}

Looking at this, it is clear that when Container() is called that the assignment to the prototype has not happened yet. 见到这种情景,很显然,当Container()被调用,即转让的原型还没有发生。 Not only that, but Container() is being called like a function and not like an instantiation . 不仅如此,还像函数一样调用Container()而不是实例化 What happens when Container is called like a function is that the this binding does not occur. 当像函数一样调用Container时,发生的情况是this绑定没有发生。 The end result is that this takes on the global reference (assuming the script is not in strict mode). 最终结果是, this采用了全局引用(假设脚本不在严格模式下)。 The global reference at this point does not have a reference to a _open property and as a result undefined is alerted and that is all that happens. 此时的全局引用没有对_open属性的引用,因此将发出undefined的警报,并且仅此而已。

In order to have this to actually alert the function _open as defined here would be to first assign the property ._open to the prototype of Container before instantiating. 为了._open实际警告功能_open,如在此定义的,将是在实例化之前首先将属性._open分配给Container的原型。 Otherwise, that property will not exist in the created object. 否则,该属性将在创建的对象中不存在。

Next, the instantiation must be used with the new keyword. 接下来,实例化必须与new关键字一起使用。 This will call the functions constructor, set up its own execution context which comes with a ThisBinding and some environments for variables. 这将调用函数构造函数,设置它自己的执行上下文,该上下文带有ThisBinding和一些变量环境。

All in all, that would look like this 总而言之,看起来像这样

//place the definition at the top for readability
//so it is obvious that it is declared first
function Container(){
    alert(this._open);
}

//Next ensure that the _open property is defined on the prototype before
//instantiating the object
Container.prototype._open = function() {
    alert("hey");
}

//Lastly, ensure that the keyword `new` is used to start the
//process of instantiation
new Container();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM