Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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对象组合与赋值&;Object.create_Javascript_Object_Composition_Assign - Fatal编程技术网

Javascript对象组合与赋值&;Object.create

Javascript对象组合与赋值&;Object.create,javascript,object,composition,assign,Javascript,Object,Composition,Assign,我正试着用它来理解javascript组合。我的基础对象上的属性在实例之间意外共享。我做错了什么?我有 Stat.js: import assign from 'object-assign'; var Stat = assign({}, { _value: undefined, get: function() { return this._value; }, set: function(n) { var max = this.

我正试着用它来理解javascript组合。我的基础对象上的属性在实例之间意外共享。我做错了什么?我有

Stat.js:

import assign from 'object-assign';
var Stat = assign({}, {
    _value: undefined,

    get: function() {
        return this._value;
    },

    set: function(n) { 
        var max = this.getMax();

        if(n > max) {
            this._value = max;
        } else {
            this._value = n; 
        }
    },

    getMax: function() {
        return 1;
    }
});
module.exports = Stat;
var Stat = {
    get: function() {
        return this._value;
    },

    set: function(n) { 
        var max = this.getMax();

        if(n > max) {
            this._value = max;
        } else {
            this._value = n; 
        }
    },

    getMax: function() {
        return 1;
    }
}
HitPoints.js:

import assign from 'object-assign'
import Stat from './Stat.js';

var HitPoints = assign({}, Stat, {        
    getMax: function() {
        return 100;
    }
});
module.exports = HitPoints;
var HitPoints = function() {
    return assign(Object.create(Stat), {    
        getMax: function() {
            return 100;
        }
    });
}
Unit.js:

import assign from 'object-assign';
import HitPoints from 'HitPoints.js';

var Unit = assign({}, {
        hp: Object.create(HitPoints)
    }
);
module.exports = Unit; 
var Unit = function() {
    return assign({},
        Object.create(XYPiece),
        Object.create(TeamMember),
        {
             hp: HitPoints()     
        }
    );                       
}
用法:

import Unit from 'Unit.js';

var u1 = Object.create(Unit);
console.log( u1.hp.get() ); // undefined - ok
u1.hp.set(11);
console.log( u1.hp.get() ); // 11 - ok

var u2 = Object.create(Unit);
console.log( u2.hp.get() ); // 11 - ???
u2.hp.set(22);
console.log( u1.hp.get() ); // 22 - ???
console.log( u2.hp.get() ); // 22
var u1 = Unit();
console.log( u1.hp.get() ); // undefined
u1.hp.set(11);
console.log( u1.hp.get() ); // 11

var u2 = Unit();
console.log( u2.hp.get() ); // undefined
u2.hp.set(22);
console.log( u1.hp.get() ); // 11
console.log( u2.hp.get() ); // 22
谢谢你的帮助……

好吧,这很有效

Stat.js:

import assign from 'object-assign';
var Stat = assign({}, {
    _value: undefined,

    get: function() {
        return this._value;
    },

    set: function(n) { 
        var max = this.getMax();

        if(n > max) {
            this._value = max;
        } else {
            this._value = n; 
        }
    },

    getMax: function() {
        return 1;
    }
});
module.exports = Stat;
var Stat = {
    get: function() {
        return this._value;
    },

    set: function(n) { 
        var max = this.getMax();

        if(n > max) {
            this._value = max;
        } else {
            this._value = n; 
        }
    },

    getMax: function() {
        return 1;
    }
}
HitPoints.js:

import assign from 'object-assign'
import Stat from './Stat.js';

var HitPoints = assign({}, Stat, {        
    getMax: function() {
        return 100;
    }
});
module.exports = HitPoints;
var HitPoints = function() {
    return assign(Object.create(Stat), {    
        getMax: function() {
            return 100;
        }
    });
}
Unit.js:

import assign from 'object-assign';
import HitPoints from 'HitPoints.js';

var Unit = assign({}, {
        hp: Object.create(HitPoints)
    }
);
module.exports = Unit; 
var Unit = function() {
    return assign({},
        Object.create(XYPiece),
        Object.create(TeamMember),
        {
             hp: HitPoints()     
        }
    );                       
}
用法:

import Unit from 'Unit.js';

var u1 = Object.create(Unit);
console.log( u1.hp.get() ); // undefined - ok
u1.hp.set(11);
console.log( u1.hp.get() ); // 11 - ok

var u2 = Object.create(Unit);
console.log( u2.hp.get() ); // 11 - ???
u2.hp.set(22);
console.log( u1.hp.get() ); // 22 - ???
console.log( u2.hp.get() ); // 22
var u1 = Unit();
console.log( u1.hp.get() ); // undefined
u1.hp.set(11);
console.log( u1.hp.get() ); // 11

var u2 = Unit();
console.log( u2.hp.get() ); // undefined
u2.hp.set(22);
console.log( u1.hp.get() ); // 11
console.log( u2.hp.get() ); // 22
文章起了作用。万岁

不过,尽管如此,如果这从根本上说是一种愚蠢的方式,告诉我…

好吧,这很有效

Stat.js:

import assign from 'object-assign';
var Stat = assign({}, {
    _value: undefined,

    get: function() {
        return this._value;
    },

    set: function(n) { 
        var max = this.getMax();

        if(n > max) {
            this._value = max;
        } else {
            this._value = n; 
        }
    },

    getMax: function() {
        return 1;
    }
});
module.exports = Stat;
var Stat = {
    get: function() {
        return this._value;
    },

    set: function(n) { 
        var max = this.getMax();

        if(n > max) {
            this._value = max;
        } else {
            this._value = n; 
        }
    },

    getMax: function() {
        return 1;
    }
}
HitPoints.js:

import assign from 'object-assign'
import Stat from './Stat.js';

var HitPoints = assign({}, Stat, {        
    getMax: function() {
        return 100;
    }
});
module.exports = HitPoints;
var HitPoints = function() {
    return assign(Object.create(Stat), {    
        getMax: function() {
            return 100;
        }
    });
}
Unit.js:

import assign from 'object-assign';
import HitPoints from 'HitPoints.js';

var Unit = assign({}, {
        hp: Object.create(HitPoints)
    }
);
module.exports = Unit; 
var Unit = function() {
    return assign({},
        Object.create(XYPiece),
        Object.create(TeamMember),
        {
             hp: HitPoints()     
        }
    );                       
}
用法:

import Unit from 'Unit.js';

var u1 = Object.create(Unit);
console.log( u1.hp.get() ); // undefined - ok
u1.hp.set(11);
console.log( u1.hp.get() ); // 11 - ok

var u2 = Object.create(Unit);
console.log( u2.hp.get() ); // 11 - ???
u2.hp.set(22);
console.log( u1.hp.get() ); // 22 - ???
console.log( u2.hp.get() ); // 22
var u1 = Unit();
console.log( u1.hp.get() ); // undefined
u1.hp.set(11);
console.log( u1.hp.get() ); // 11

var u2 = Unit();
console.log( u2.hp.get() ); // undefined
u2.hp.set(22);
console.log( u1.hp.get() ); // 11
console.log( u2.hp.get() ); // 22
文章起了作用。万岁


尽管如此,如果这从根本上说是一种愚蠢的方式,请告诉我……

对于初学者来说,这是一个为什么人们不希望您使用
类的快速示例
我并不一定讨厌
,但使用
的90%原因是为了获得继承,虽然它偶尔会有帮助,但通常会非常痛苦

class Person { }


class ArmedPerson extends Person {
  constructor (details) {
    super(details);
    const gun = new Gun();
    this.equipment = { gun };
  }

  attack (target) {
    this.equipment.gun.fireAt(target);
  }
}


class Civilian extends Person { }


class ArmedCivilian extends ArmedPerson {
  /* ... shouldn't it be extending Civilian?
    Maybe all Civilians should be armed?
    Is this why most games take place in the US?
  */
}


class Soldier extends ArmedPerson {
  constructor (personDetails) {
    super(personDetails);
  }
}


class UnarmedSoldier extends Soldier {
  /* HOW DO I TAKE HIS GUN AWAY? */
  constructor (personDetails) {
    super(personDetails);
  }
  attack () {
    /* I know he has a gun, and anybody hacking the game can use it, but what do I do here? */
  }
}
class
在过去的30多年里,继承已经成为人们严重滥用的东西之一(就像所有其他有用的工具一样)

我们可以查看组合(通过依赖项反转),而不是继承

就我们想要的最终结果而言,没有太多变化。。。我们已经能够简化很多,现在我们甚至可以给他一种方法,如果我们愿意,可以交换枪支,所有这些都是因为我们不是把枪栓在他继承的东西上,而是在我们第一次见到他时递给他一把枪。 它可以是我们想要的任何类型的枪,只要它仍然可以以类似的方式发射

问题是: 那么,如果继承完全不存在的话,有没有更好的方法使事物可重用呢

对此,我要说:继承不应该被完全排除在外。。。当你发现这真的是完成某件事的最好和最干净的方式时(而不是试图继承……某件事,任何事,现在!),这应该是一个“啊哈”的时刻

各种语言都有一个称为特征或混合的概念。
在Java之类的语言中,接口是一种近似值。
我不太喜欢界面(结构,而不是概念-喜欢概念)。
在Java中,接口让你做更多的工作,因为它们让你定义函数,函数取什么,函数返回什么。。。 …但是您不能给它任何默认行为(传统上),因此,如果您有14个实现相同接口的对象,那么这就是您编写14次的相同方法(加上接口的签名)。有时,这些方法在具体的实施细节上会完全不同;好的。。。有时,它们与您开始编写接口时的预期完全相同

那不太好。队列特征;这些是您定义的接口、行为,然后复制到对象上的内容。 在JS中,我们甚至可以通过注入他们开始工作的上下文,而不是让他们假设自己在处理整个
这个
,从而在他们周围有一些关闭安全性

const Movable = (pos) => ({
  up (distance) { pos.y += distance; },
  down (distance) { pos.y -= distance; },
  left (distance) { pos.x -= distance; },
  right (distance) { pos.x += distance; }
});


class Point {
  constructor (x, y) {
    Object.assign(this, { x, y });
  }
}

class Person {
  constructor (position) {
    Object.assign(this, { position }, Movable(position));
  }
}


const person = new Person( new Point(0, 0) );

person.up( 20 );
person.position.y; // 20
请注意,Movable正在返回一个对象的新实例,其方法更改
位置上的值。该对象正在将其方法复制到person实例上


现在,我可以创建任意数量的这些特性,并将它们复制到任意数量的对象上,并以这种方式进行重用。

对于初学者,这是一个简单的示例,说明人们为什么不希望您使用

我并不一定讨厌
,但使用
的90%原因是为了获得继承,虽然它偶尔会有帮助,但通常会非常痛苦

class Person { }


class ArmedPerson extends Person {
  constructor (details) {
    super(details);
    const gun = new Gun();
    this.equipment = { gun };
  }

  attack (target) {
    this.equipment.gun.fireAt(target);
  }
}


class Civilian extends Person { }


class ArmedCivilian extends ArmedPerson {
  /* ... shouldn't it be extending Civilian?
    Maybe all Civilians should be armed?
    Is this why most games take place in the US?
  */
}


class Soldier extends ArmedPerson {
  constructor (personDetails) {
    super(personDetails);
  }
}


class UnarmedSoldier extends Soldier {
  /* HOW DO I TAKE HIS GUN AWAY? */
  constructor (personDetails) {
    super(personDetails);
  }
  attack () {
    /* I know he has a gun, and anybody hacking the game can use it, but what do I do here? */
  }
}
class
在过去的30多年里,继承已经成为人们严重滥用的东西之一(就像所有其他有用的工具一样)

我们可以查看组合(通过依赖项反转),而不是继承

就我们想要的最终结果而言,没有太多变化。。。我们已经能够简化很多,现在我们甚至可以给他一种方法,如果我们愿意,可以交换枪支,所有这些都是因为我们不是把枪栓在他继承的东西上,而是在我们第一次见到他时递给他一把枪。 它可以是我们想要的任何类型的枪,只要它仍然可以以类似的方式发射

问题是: 那么,如果继承完全不存在的话,有没有更好的方法使事物可重用呢

对此,我要说:继承不应该被完全排除在外。。。当你发现这真的是完成某件事的最好和最干净的方式时(而不是试图继承……某件事,任何事,现在!),这应该是一个“啊哈”的时刻

各种语言都有一个称为特征或混合的概念。
在Java之类的语言中,接口是一种近似值。
我不太喜欢界面(结构,而不是概念-喜欢概念)。
在Java中,接口让你做更多的工作,因为它们让你定义函数,函数取什么,函数返回什么。。。 …但是您不能给它任何默认行为(传统上),因此,如果您有14个实现相同接口的对象,那么这就是您编写14次的相同方法(加上接口的签名)。有时,这些方法在具体的实施细节上会完全不同;好的。。。有时,它们与您开始编写接口时的预期完全相同

那不太好。队列特征;这些是您定义的接口、行为,然后复制到您的对象上的内容