相关疑难解决方法(0)

JavaScript中的'new'关键字是什么?

newJavaScript中的关键字在第一次遇到时会非常混乱,因为人们倾向于认为JavaScript不是面向对象的编程语言.

  • 它是什么?
  • 它解决了什么问题?
  • 什么时候适当,什么时候不适合?

javascript new-operator

1684
推荐指数
12
解决办法
25万
查看次数

原型继承 - 写作

所以我有这两个例子,来自javascript.info:

例1:

var animal = {
  eat: function() {
    alert( "I'm full" )
    this.full = true
  }
}

var rabbit = {
  jump: function() { /* something */ }
}

rabbit.__proto__ = animal 

rabbit.eat() 
Run Code Online (Sandbox Code Playgroud)

例2:

function Hamster() {  }
Hamster.prototype = {
  food: [],
  found: function(something) {
    this.food.push(something)
  }
}

// Create two speedy and lazy hamsters, then feed the first one
speedy = new Hamster()
lazy = new Hamster()

speedy.found("apple")
speedy.found("orange")

alert(speedy.food.length) // 2
alert(lazy.food.length) // 2 (!??)
Run Code Online (Sandbox Code Playgroud)

从示例2开始:当代码到达时 …

javascript inheritance prototype

130
推荐指数
2
解决办法
1万
查看次数

什么时候JavaScript对象被破坏?

在C++中,我可以显式定义构造函数和析构函数,然后在构造函数/析构函数中使用cout <<"C或D Called"来确切知道在哪里.

但是在JavaScript中如何知道对象何时被破坏.以下示例是关注我的案例.

我在超时时调用内部函数,我想知道只要定时器正在运行,对象就会保持活动状态,等待再次调用next.

用户点击呼叫控制

// Calls  Control
Run Code Online (Sandbox Code Playgroud)

控制呼叫消息

var message_object = new Message( response_element );
Run Code Online (Sandbox Code Playgroud)

消息调用效果

new Effects().fade( this.element, 'down', 4000 );
message_object.display( 'empty' );
Run Code Online (Sandbox Code Playgroud)

效果

/**
 *Effects - build out as needed
 *  element - holds the element to fade
 *  direction - determines which way to fade the element
 *  max_time - length of the fade
 */

var Effects = function(  ) 
{
    this.fade = function( element, direction, max_time ) 
    {
        element.elapsed = 0;
        clearTimeout( element.timeout_id …
Run Code Online (Sandbox Code Playgroud)

javascript

25
推荐指数
2
解决办法
2万
查看次数

this.constructor.prototype - 不能完全覆盖,但可以写个别道具吗?

TL; DR? 为什么我不能在构造函数中覆盖构造函数的原型?

我正在弄清楚我的原型继承模式.我不喜欢原型通常是从​​构造函数外部定义的,并希望逻辑上更好地封装事物.

我发现我期望工作的一条神奇的线路没有.

function Orifice(){
  this.exhaust=function(){};
  this.ingest=function(){};
}
var standardOrifice = new Orifice();

function Sphincter(){
  this.constructor.prototype = standardOrifice; // <-- does not work
  this.relax=function(){};
  this.tighten=function(){};
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,我可以编写单独的属性this.constructor.prototype,但是我不能像构造函数定义之外那样覆盖整个原型对象.

所以像这样的东西工作:

  this.constructor.prototype.exhaust = standardOrifice.exhaust;
  this.constructor.prototype.ingest = standardOrifice.ingest;
Run Code Online (Sandbox Code Playgroud)

为此我可以创建一个简单的克隆函数来处理这个:

function extend(target){
  return {
    from: function(obj){
      target.__proto__ = obj.constructor.prototype;
      for (key in obj) if (obj.hasOwnProperty(key)) target[key]=obj[key];
      return target;
    }
  };
}
Run Code Online (Sandbox Code Playgroud)

值得庆幸的是,到目前为止,在我的测试中,这种技术似乎运行良好,但我不确定是否存在我可能遗漏的细节或性能案例.

function Sphincter(){
  extend(this.constructor.prototype).from(standardOrifice);
  //...
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能在构造函数中覆盖构造函数的原型?但我可以在构造函数之外?单独编写属性是否在构造函数中起作用?

javascript

1
推荐指数
1
解决办法
204
查看次数

标签 统计

javascript ×4

inheritance ×1

new-operator ×1

prototype ×1