JavaScript 继承和数组
我这样定义我的类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要
应用
“ “继承”构造函数中的“父”构造函数:对“继承”构造函数中的所有执行相同的操作:
请参阅 jsfiddle 上的示例。
正如 @austincheney 指出的那样,JavaScript 没有“类”——只有函数(即对象)、构造函数(即函数)和对象。
JavaScript 使用原型继承。这意味着当您尝试访问不存在的对象的属性(或函数)时,它将委托给原型对象。
考虑:
这里的输出是:
对象
neilDeGrasseTyson
尚未继承name
属性。它根本就没有一个。由于它没有属性name
,当我们尝试访问name
时,neilDeGrasseTyson
对象将委托给原型对象Scientist。原型
,并返回Scientist.prototype.name
的值,即isaacNewton.name
。在您的代码中,对象
mark
、louis
、ricky
和john
不会有一个属性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:Do the same for all your "inherited" constructor functions:
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:
The output here is:
Object
neilDeGrasseTyson
hasn't inherited thename
property. It simply doesn't have one. Since it doesn't have a propertyname
when we try to accessname
theneilDeGrasseTyson
object will delegate to the prototype objectScientist.prototype
, and return the value ofScientist.prototype.name
which isisaacNewton.name
.In your code, objects
mark
,louis
,ricky
andjohn
don't have a propertyval
. All those calls tosetVal
end up manipulatingWorkerBee.prototype.val
since none of those objects have their ownval
property. By applying theEmployee
constructor to them, you introduce the properties of anEmployee
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
inEmployee
and give each "inheriting" constructor athis.val
property: http://jsfiddle.net/FDCXF/1/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.