JavaScript 继承和数组

发布于 2024-12-18 12:26:20 字数 2007 浏览 1 评论 0原文

我这样定义我的类:

function Employee () {
    this.name = "";
    this.val = new Array();
}

function WorkerBee () {
    this.beeQueen = "lola";

    this.setVal = function(val) {
        this.val.push(val);
    };
}
WorkerBee.prototype = new Employee;


function SalesPerson () {
    this.dept = "development";  
}
SalesPerson.prototype = new WorkerBee;

function Engineer () {
    this.dept = "R&D";
}
Engineer.prototype = new WorkerBee;

问题:我创建的所有对象共享相同的 val 数组:

var mark = new WorkerBee;
mark.name = "Mark";
mark.setVal('00000')

var louis = new SalesPerson;
louis.name = "Louis";
louis.setVal('11111')

var ricky = new SalesPerson;
ricky.name = "Ricky";
ricky.setVal('33333')

var john = new Engineer;
john.name = "John";
john.setVal('55555');

此:

html += "<br /><br />Name: " + mark.name;
html += "<br />Val: " + mark.val;

html += "<br /><br />Name: " + louis.name;
html += "<br />Val: " + louis.val;

html += "<br /><br />Name: " + ricky.name;
html += "<br />Val: " + ricky.val;

html += "<br /><br />Name: " + john.name;
html += "<br />Val: " + john.val;

显示:

Name: Mark
Val: 00000,11111,33333,55555

Name: Louis
Val: 00000,11111,33333,55555

Name: Ricky
Val: 00000,11111,33333,55555

Name: John
Val: 00000,11111,33333,55555

我读过 http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/http://javascriptweblog.wordpress.com/2010/06/07/understanding- javascript-prototypes/ 但我还是很困惑!

当我使用字符串而不是数组时,这效果很好(因为我想对字符串的引用被覆盖),但是如何使用数组来做到这一点?

这样我就可以拥有:

姓名:Mark 值:00000

姓名:路易斯 验证码:11111

姓名:瑞奇 验证码:33333

姓名:约翰 价值:55555

I defined my classes like that:

function Employee () {
    this.name = "";
    this.val = new Array();
}

function WorkerBee () {
    this.beeQueen = "lola";

    this.setVal = function(val) {
        this.val.push(val);
    };
}
WorkerBee.prototype = new Employee;


function SalesPerson () {
    this.dept = "development";  
}
SalesPerson.prototype = new WorkerBee;

function Engineer () {
    this.dept = "R&D";
}
Engineer.prototype = new WorkerBee;

Problem: all the objects I create share the same val array:

var mark = new WorkerBee;
mark.name = "Mark";
mark.setVal('00000')

var louis = new SalesPerson;
louis.name = "Louis";
louis.setVal('11111')

var ricky = new SalesPerson;
ricky.name = "Ricky";
ricky.setVal('33333')

var john = new Engineer;
john.name = "John";
john.setVal('55555');

This:

html += "<br /><br />Name: " + mark.name;
html += "<br />Val: " + mark.val;

html += "<br /><br />Name: " + louis.name;
html += "<br />Val: " + louis.val;

html += "<br /><br />Name: " + ricky.name;
html += "<br />Val: " + ricky.val;

html += "<br /><br />Name: " + john.name;
html += "<br />Val: " + john.val;

displays:

Name: Mark
Val: 00000,11111,33333,55555

Name: Louis
Val: 00000,11111,33333,55555

Name: Ricky
Val: 00000,11111,33333,55555

Name: John
Val: 00000,11111,33333,55555

I read http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ and http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/ but I'm still confused!

When I use a string instead of an array this works well (because the reference to the string is overwritten I suppose) but how to do it with array ?

So I can have:

Name: Mark
Val: 00000

Name: Louis
Val: 11111

Name: Ricky
Val: 33333

Name: John
Val: 55555

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城泪 2024-12-25 12:26:20

您需要应用“ “继承”构造函数中的“父”构造函数:

function WorkerBee () {
    Employee.apply(this);
    /*...*/
}
WorkerBee.prototype = new Employee();

对“继承”构造函数中的所有执行相同的操作:

function SalesPerson () {
    WorkerBee.apply(this);
    /*...*/
}
SalesPerson.prototype = new WorkerBee();

function Engineer () {
    WorkerBee.apply(this);
    /*...*/
}
Engineer.prototype = new WorkerBee();

请参阅 jsfiddle 上的示例

正如 @austincheney 指出的那样,JavaScript 没有“类”——只有函数(即对象)、构造函数(即函数)和对象。


JavaScript 使用原型继承。这意味着当您尝试访问不存在的对象的属性(或函数)时,它将委托给原型对象。

考虑:

var isaacNewton = {
    name: 'Isaac Newton'
};

function Scientist() {}
Scientist.prototype = isaacNewton;

var neilDeGrasseTyson = new Scientist();

console.log(neilDeGrasseTyson.name);

isaacNewton.name = 'Sir Isaac Newton';

console.log(neilDeGrasseTyson.name);

这里的输出是:

Isaac Newton
Sir Isaac Newton

对象 neilDeGrasseTyson 尚未继承 name 属性。它根本就没有一个。由于它没有属性 name,当我们尝试访问 name 时,neilDeGrasseTyson 对象将委托给原型对象 Scientist。原型,并返回Scientist.prototype.name的值,即isaacNewton.name

您的代码中,对象marklouisrickyjohn不会有一个属性val。所有这些对 setVal 的调用最终都会操作 WorkerBee.prototype.val,因为这些对象都没有自己的 val 属性。通过将 Employee 构造函数应用于它们,您可以向它们引入 Employee 的属性,因此它们不必进行委托。

为了更清楚地说明这一点,另一个解决方案是将方法 setVal 放入 Employee 中,并为每个“继承”构造函数提供一个 this.val< /code> 属性: http://jsfiddle.net/FDCXF/1/

You need to apply the "parent" constructor in your "inheriting" constructor functions:

function WorkerBee () {
    Employee.apply(this);
    /*...*/
}
WorkerBee.prototype = new Employee();

Do the same for all your "inherited" constructor functions:

function SalesPerson () {
    WorkerBee.apply(this);
    /*...*/
}
SalesPerson.prototype = new WorkerBee();

function Engineer () {
    WorkerBee.apply(this);
    /*...*/
}
Engineer.prototype = new WorkerBee();

See the example on jsfiddle.

And like @austincheney pointed out, JavaScript has no "classes" - only functions (which are objects), constructors (which are functions) and objects.


JavaScript uses prototypal inheritance. This means that when you try to access a property (or function) of an object which doesn't exist, it will delegate to the prototype object.

Consider:

var isaacNewton = {
    name: 'Isaac Newton'
};

function Scientist() {}
Scientist.prototype = isaacNewton;

var neilDeGrasseTyson = new Scientist();

console.log(neilDeGrasseTyson.name);

isaacNewton.name = 'Sir Isaac Newton';

console.log(neilDeGrasseTyson.name);

The output here is:

Isaac Newton
Sir Isaac Newton

Object neilDeGrasseTyson hasn't inherited the name property. It simply doesn't have one. Since it doesn't have a property name when we try to access name the neilDeGrasseTyson object will delegate to the prototype object Scientist.prototype, and return the value of Scientist.prototype.name which is isaacNewton.name.

In your code, objects mark, louis, ricky and john don't have a property val. All those calls to setVal end up manipulating WorkerBee.prototype.val since none of those objects have their own val property. By applying the Employee constructor to them, you introduce the properties of an Employee to them, so they don't have to delegate.

To drive the point home a little more, another solution would have been to put the method setVal in Employee and give each "inheriting" constructor a this.val property: http://jsfiddle.net/FDCXF/1/

最冷一天 2024-12-25 12:26:20

1) JavaScript 没有类。

2)JavaScript只有函数作用域,没有块作用域。

3) 变量仅通过“var”关键字提供作用域。

了解这一点后重新设计您的代码,看看您是否仍然遇到此问题。

1) JavaScript does not have classes.

2) JavaScript only has function scope and not block scope.

3) A variable is only provided scope with the "var" keyword.

Re-engineer your code knowing this and see if you still encounter this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文