Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回与预期不同的内容的Javascript构造函数方法_Javascript - Fatal编程技术网

返回与预期不同的内容的Javascript构造函数方法

返回与预期不同的内容的Javascript构造函数方法,javascript,Javascript,因此,在我的代码中,我尝试使用原型继承。每次我创建一个新的Fruit实例(var aFruit=new Fruit()),新实例的原型都会从Fruit构造函数中分配原型,即Fruit.prototype 那么为什么是阿巴纳纳呢 不是 但是 我认为构造函数方法就是这样做的: 此外,从另一个对象继承的所有对象也继承构造函数属性。这个构造函数属性只是一个属性(像任何变量一样),它保存或指向对象的构造函数 该代码有两个问题: 使用newplant()创建水果。原型是一种非常常见的反模式;相反,使用Obj

因此,在我的代码中,我尝试使用原型继承。每次我创建一个新的Fruit实例(var aFruit=new Fruit()),新实例的原型都会从Fruit构造函数中分配原型,即Fruit.prototype

那么为什么是阿巴纳纳呢 不是

但是

我认为构造函数方法就是这样做的:

此外,从另一个对象继承的所有对象也继承构造函数属性。这个构造函数属性只是一个属性(像任何变量一样),它保存或指向对象的构造函数


该代码有两个问题:

  • 使用
    newplant()
    创建
    水果。原型是一种非常常见的反模式;相反,使用
    Object.create
    并从
    Fruit
    中调用
    Plant
    。在您的特定代码中,这并不重要,但如果您想从
    水果
    中获得一些东西,或者您想将
    国家
    作为
    植物
    的参数,这将很重要

  • 如果希望它指向
    水果
    ,则需要在
    水果.prototype上设置
    构造函数

  • 因此:

    当然,从ES2015开始,我们有
    语法,您现在可以使用transpiler(或者如果您只需要支持当前版本的Chrome和Firefox):

    我认为构造函数方法就是这样做的:

    此外,从另一个对象继承的所有对象也继承构造函数属性。这个构造函数属性只是一个属性(像任何变量一样),它保存或指向对象的构造函数

    constructor
    不是一个方法,而是一个属性,它引用了
    prototype
    对象相关的函数。JavaScript本身根本不使用
    constructor
    进行任何操作,但定义了对于所有具有
    prototype
    属性的函数,当函数首次创建时,
    prototype
    属性指向的对象将具有指向函数的
    constructor
    属性。但是,由于您将
    prototype
    的值替换为对不同对象的引用,因此必须更新
    constructor
    属性,使其再次指向正确的函数(如果您希望彻底,这是最好的,即使JavaScript不使用它,也不意味着库不使用它)


    在非常旧的浏览器上,您可能需要填充
    对象。创建
    。它不能完全加垫片,但足以满足上述要求:

    class Plant {
        constructor() {
            this.country = "Mexico";
            this.isOrganic = true;
    }
    
    class Fruit extends Plant {
        constructor(fName, fColor) {
            super();
            this.name = fName;
            this.color = fColor;
        }
    }
    

    该代码有两个问题:

  • 使用
    newplant()
    创建
    水果。原型是一种非常常见的反模式;相反,使用
    Object.create
    并从
    Fruit
    中调用
    Plant
    。在您的特定代码中,这并不重要,但如果您想从
    水果
    中获得一些东西,或者您想将
    国家
    作为
    植物
    的参数,这将很重要

  • 如果希望它指向
    水果
    ,则需要在
    水果.prototype上设置
    构造函数

  • 因此:

    当然,从ES2015开始,我们有
    语法,您现在可以使用transpiler(或者如果您只需要支持当前版本的Chrome和Firefox):

    我认为构造函数方法就是这样做的:

    此外,从另一个对象继承的所有对象也继承构造函数属性。这个构造函数属性只是一个属性(像任何变量一样),它保存或指向对象的构造函数

    constructor
    不是一个方法,而是一个属性,它引用了
    prototype
    对象相关的函数。JavaScript本身根本不使用
    constructor
    进行任何操作,但定义了对于所有具有
    prototype
    属性的函数,当函数首次创建时,
    prototype
    属性指向的对象将具有指向函数的
    constructor
    属性。但是,由于您将
    prototype
    的值替换为对不同对象的引用,因此必须更新
    constructor
    属性,使其再次指向正确的函数(如果您希望彻底,这是最好的,即使JavaScript不使用它,也不意味着库不使用它)


    在非常旧的浏览器上,您可能需要填充
    对象。创建
    。它不能完全加垫片,但足以满足上述要求:

    class Plant {
        constructor() {
            this.country = "Mexico";
            this.isOrganic = true;
    }
    
    class Fruit extends Plant {
        constructor(fName, fColor) {
            super();
            this.name = fName;
            this.color = fColor;
        }
    }
    

    该代码有两个问题:

  • 使用
    newplant()
    创建
    水果。原型是一种非常常见的反模式;相反,使用
    Object.create
    并从
    Fruit
    中调用
    Plant
    。在您的特定代码中,这并不重要,但如果您想从
    水果
    中获得一些东西,或者您想将
    国家
    作为
    植物
    的参数,这将很重要

  • 如果希望它指向
    水果
    ,则需要在
    水果.prototype上设置
    构造函数

  • 因此:

    当然,从ES2015开始,我们有
    语法,您现在可以使用transpiler(或者如果您只需要支持当前版本的Chrome和Firefox):

    我认为构造函数方法就是这样做的:

    此外,从另一个对象继承的所有对象也继承构造函数属性。这个构造函数属性只是一个属性(像任何变量一样),它保存或指向对象的构造函数

    constructor
    不是方法,而是引用函数的属性
    [function: Plant]?
    
    function Plant() {
      this.country = "Mexico"
      this.isOrganic = true;
    }
    
    function Fruit(fName, fColor) {
      Plant.call(this);                               // **
      this.name = fName;
      this.color = fColor;
    }
    
    Fruit.prototype = Object.create(Plant.prototype); // **
    Fruit.prototype.constructor = Fruit;              // **
    
    class Plant {
        constructor() {
            this.country = "Mexico";
            this.isOrganic = true;
    }
    
    class Fruit extends Plant {
        constructor(fName, fColor) {
            super();
            this.name = fName;
            this.color = fColor;
        }
    }
    
    if (!Object.create) {
        Object.create = function(p, props) {
            if (typeof props !== "undefined") {
                throw new Error("The second argument of Object.create cannot be shimmed.");
            }
            function ctor() { }
            ctor.prototype = p;
            return new ctor;
        };
    }