相关疑难解决方法(0)

Javascript多重继承

任何人都可以请帮助以下代码.我试图理解多重继承不确定为什么它不起作用.BTW如果是多重继承的代码.谢谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Test Doc</title>
    <script type="text/javascript">
    function classX(){
        this.messageX="this is X Message";
        this.alertX=function(){
            alert(this.messageX);
        };
    }
    function classY(){
        this.messageY="this is Y Message";
        this.alertY=function(){
            alert(this.messageY);
        };
    }
    function classZ(){
        classX.apply(this);
        classY.apply(this);
        this.messageZ="this is Z Message";
        this.alertZ=function(){
            alert(this.messageZ);
        };
    }
    var abjz=new classZ();
    objz.alertZ();
    abjz.alertX();
    </script>
</head>

<body>


</body>
</html>
Run Code Online (Sandbox Code Playgroud)

javascript inheritance

5
推荐指数
2
解决办法
8833
查看次数

为什么我不能在我的javascript代码中继承Animal类的原型?

我正在尝试创建一个Dog通过类的原型继承继承的新Animal类:

function Animal() {
  this.name = "animal";
  this.writeName = function() {
    document.write(this.name);
  }    
}

function Dog() {
  this.name = "dog";
  this.prototype = new Animal();
}

new Dog().writeName()
Run Code Online (Sandbox Code Playgroud)

JS小提琴

但是,我收到一个Javascript错误:Uncaught TypeError: Object #<Dog> has no method 'say'.

为什么?Dog对象不应该将对象保留Animal为原型吗?

javascript

5
推荐指数
2
解决办法
495
查看次数

JS实现"扩展"功能(真正的简单继承)

说我在JS中有原型函数的classe ...

function Foo() {
    this.stuff = 7;
    this.otherStuff = 5;
}

Foo.prototype.doSomething = function() { };

Foo.prototype.doSomethingElse = function() { };
Run Code Online (Sandbox Code Playgroud)

现在说我想通过继承它来"扩展"这个类.在Java中,这看起来像......

public class Bar extends Foo {}
Run Code Online (Sandbox Code Playgroud)

现在我知道在JS中确实没有类的概念,一切都可以改变,而且这一切真的只是归结为一大堆字典但是,尽管如此,我应该能够复制一个类的原型并附加它对另一个原型,对吧?

在香草JS中,这样的代码会是什么样子?

javascript

5
推荐指数
2
解决办法
1261
查看次数

JavaScript和原型继承

好像我终于理解了JavaScript继承以及如何正确地完成它.这是我的代码:

function Human(eyes) {
    this.eyes = eyes ? "Not blind" : "Blind";
}
Human.prototype.canSee = function () {
    return this.eyes;
};
function Male(name, eyes) {
    Human.call(this, eyes);
    this.name = name;
}
Male.prototype = Object.create(Human.prototype);
var Sethen = new Male("Sethen", true);
console.log(Sethen.canSee()); //logs "Not blind"
Run Code Online (Sandbox Code Playgroud)

据我所知,使用Object.create创建继承的原型对象比使用new关键字要好得多.这引起了我脑子里的几个问题.

  1. Male.prototype = Object.create(Human.prototype)将原型链Male.prototype --> Human.prototype --> Object.prototype --> null
  2. MaleHuman.call(this, eyes);用来调用超类的构造函数中,我必须在Male构造函数中再次传递眼睛以将其传递给Human构造函数.这似乎很痛苦,有没有更简单的方法呢?
  3. 为什么有时候我会看到代码Male.prototype = new Human();...这似乎是不正确的.当我们这样做时,实际发生了什么?

javascript

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

为什么不向JavaScript构造函数添加功能,而是通过原型添加?

我看看Addy Osmani关于构造函数模式的章节:http://addyosmani.com/resources/essentialjsdesignpatterns/book/#constructorpatternjavascript 我发现了以下内容:

function Car( model, year, miles ) {

  this.model = model;
  this.year = year;
  this.miles = miles;

  this.toString = function () {
    return this.model + " has done " + this.miles + " miles";
  };
}

// Usage:

// We can create new instances of the car
var civic = new Car( "Honda Civic", 2009, 20000 );
var mondeo = new Car( "Ford Mondeo", 2010, 5000 );

// and then open our browser console to …
Run Code Online (Sandbox Code Playgroud)

javascript constructor

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

揭示Prototype Pattern私有变量

我在JavaScript的Revealing Prototype Pattern中遇到私有变量的问题.我无法弄清楚如何在共享(单例)原型中使用多个不同函数中使用的私有变量,而不暴露它们.这是我在JSFiddle中的意思的例子.

问题在于使用var vthis.v.第一个是混淆所有实例的状态,但第二个是公开可见的.有没有办法让v private,并为每个单独的实例保留其状态?

javascript revealing-prototype

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

JavaScript更好的方法来修改函数原型

我希望创建一个构造函数的构造函数.关于这个线程:JavaScript构建构造函数的构造函数,似乎唯一的解决方案是:

Function.prototype.add = function(name, value) {
    this.prototype[name] = value;
};
Function.prototype.remove = function(name) {
    delete this.prototype[name];
};
Run Code Online (Sandbox Code Playgroud)

但我不想修改通用Function原型......还有:

var A = new ConstBuilder().add('test', function() {
    console.log('test');
}).getConstructor();
Run Code Online (Sandbox Code Playgroud)

但我不想在构造函数本身周围有一个对象包装器.

问题是通常构造函数创建新对象,从构造函数原型继承方法.我想要做的是实现函数而不是对象,但修改函数原型属性的唯一方法是修改其__proto__属性:

var constructorPrototype = {
    add : function(name, value) {
        this.prototype[name] = value ;
    }
} ;

var ConstBuilder = function() {
    var constructor = function() {} ;
    constructor.prototype = {} ;
    // The only way (?), but quite deprecated...
    constructor.__proto__ = constructorPrototype ;
    return constructor …
Run Code Online (Sandbox Code Playgroud)

javascript constructor prototype

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

JS类没有经常引用"this"进行成员访问

我典型的JS类结构如下所示:

MyClass = function(args)
{
   this.myProp1 = undefined;
   this.myProp2 = args[0];
   //...more member data 

   this.foo = function()
   {
       return this.myProp1 + this.myProp2; //<- the problem.
   }
   //...more member functions
}

//if MyClass extends a superclass, add the following...
MyClass.prototype = Object.create(MySuperClass.prototype);
MyClass.prototype.constructor = MyClass;
Run Code Online (Sandbox Code Playgroud)

...我对JS的持续烦恼是我似乎必须this在成员函数中不断使用才能访问这些函数所属的同一对象的属性.在this成员函数处理同一类/实例中的成员数据时,可以安全地省略其他几种语言,例如C#和Java .(我意识到JS的结构根本不同,因为它被设计为原型而不是分层继承语言.)

用另一种方式提出问题:有没有办法让未指明的范围不指向window,而是指当前的本地值this

PS我猜这是一种语言限制,但我想我还是要再检查一下.

javascript oop class this

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

javascript 构造函数和object.create 可以结合使用吗?

更新

如果这是不可能的,请随时提供解释原因的答案。我很乐意将其标记为已接受。


我想稍微简化以下代码(对象“声明”的两个步骤,我想要一个):

var Masher = function(opts) {
    this._name  = opts.name;
};

Masher.prototype = Object.create(Object.prototype, {
    _name:  { writable: true },
    name:  { get: function() { return this._name;  }}
});

// Note: (new Masher({name: 'bar'})).name == 'bar'
Run Code Online (Sandbox Code Playgroud)

我想一次性创建整个函数原型,构造函数出现在 Object.create 的某处。也许,像这样:

var Basher = Object.create(Function.prototype, {
    _name:  { writable: true },
    name:  { get: function() { return this._name;  }},
    constructor: { value: function(opts) { this._name = opts.name; }}
});
Run Code Online (Sandbox Code Playgroud)

但是,当我调用时new Basher(),我得到:'TypeError: object is not a function'。

虽然我意识到我可以用语法糖(一个帮助程序库)来做到这一点,但我的目标是让事情尽可能简单,并对 JS 对象、原型、构造函数内部结构有一些了解。我试图阅读尽可能多的内容: …

javascript constructor new-operator object-create

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

Javascript Module Pattern使用Prototype创建多个实例

我想要实现的目标:使用javascript中的原型创建模块,以便用户可以使用不同的选项多次实例化模块.

问题:当使用var my_module3 = new module();然后尝试设置选项my_module3.init({ option: "value" });时,每次都不更改对象,它只更改一次.

测试:使用时console.log我们可以看到它打印出具有相同选项的两个对象,即使它们设置不同

Object {first: "Barry", second: "Larry", third: "Sam"} 
Object {first: "Barry", second: "Larry", third: "Sam"} 
Run Code Online (Sandbox Code Playgroud)

这是我的jsFiddle完整代码: http ://jsfiddle.net/11bLouc8/2/

        var module = (function () {
        // default options
        var options = {
            first: "test",
            second: "test2",
            third: "test3"
        };
        // take in useroptions and replace default options
        var module = function(userOptions) {
            if (userOptions != null && userOptions != undefined
                && userOptions …
Run Code Online (Sandbox Code Playgroud)

html javascript performance jquery html5

5
推荐指数
2
解决办法
2799
查看次数