JavaScript中的多态性示例使用错误
这是一个演示多态性的示例,如果我删除以下行:JavaScript中的多态性示例使用错误,javascript,function,oop,polymorphism,Javascript,Function,Oop,Polymorphism,这是一个演示多态性的示例,如果我删除以下行: Employee.prototype= new Person(); Employee.prototype.constructor=Employee; 对程序仍然没有影响,并且得到了类似的结果。如果是这样的话,这个示例如何演示多态性?评论这些行,我看到有两个函数,它们在调用返回结果时基于自己的getInfo函数;那么,魔法在哪里 HTML: <script type="text/javascript"> function Person
Employee.prototype= new Person();
Employee.prototype.constructor=Employee;
对程序仍然没有影响,并且得到了类似的结果。如果是这样的话,这个示例如何演示多态性?评论这些行,我看到有两个函数,它们在调用返回结果时基于自己的getInfo函数;那么,魔法在哪里
HTML:
<script type="text/javascript">
function Person(age, weight) {
this.age=age;
this.weight=weight;
this.getInfo=function() {
return "I am " + this.age + " years old " +
"and weighs " + this.weight +" kilo.";
}
}
function Employee(age, weight, salary){
this.salary=salary;
this.age=age;
this.weight=weight;
this.getInfo=function() {
return "I am " + this.age + " years old " +
"and weighs " + this.weight +" kilo " +
"and earns " + this.salary + " dollar.";
}
}
Employee.prototype= new Person();
Employee.prototype.constructor=Employee;
// The argument, 'obj', can be of any kind
// which method, getInfo(), to be executed depend on the object
// that 'obj' refer to.
function showInfo(obj) {
document.write(obj.getInfo()+"<br>");
}
var person = new Person(50,90);
var employee = new Employee(43,80,50000);
showInfo(person);
showInfo(employee);
</script>
职能人员(年龄、体重){
这个。年龄=年龄;
这个。重量=重量;
this.getInfo=function(){
返回“我是”+这个年龄+“岁”+
重量为“+这个重量+”公斤。”;
}
}
职能员工(年龄、体重、工资){
这个。薪水=薪水;
这个。年龄=年龄;
这个。重量=重量;
this.getInfo=function(){
返回“我是”+这个年龄+“岁”+
重量为“+此重量+千克”+
“并赚取“+这个工资+”美元。”;
}
}
Employee.prototype=新员工();
Employee.prototype.constructor=Employee;
//参数“obj”可以是任何类型的
//要执行的方法getInfo()取决于对象
//“obj”指的是。
功能显示信息(obj){
document.write(obj.getInfo()+“
”;
}
var人员=新人员(50,90);
var员工=新员工(43,8050000);
showInfo(个人);
showInfo(员工);
结果
参考
这是多态性的一个很弱的例子,除了您已经注意到的问题(即
Employee.prototype
设置非常出色,示例中从未使用过prototype函数)之外,我还看到了一些其他问题:
Employee
函数(即构造函数)从不调用“基类”构造函数,即Person
函数-如果它们处于父子关系中,您可能会认为这是正确的Employee
函数从父函数粘贴了代码副本:
this.salary=salary;
这个。年龄=年龄;
这个。重量=重量代码>
getInfo
函数的Idem-两个构造函数都创建它们的特定函数并将其分配给此
;而getInfo
的Employee
版本从不调用基类版本——它可能应该调用基类版本来演示继承
因此,实际上,唯一有利于此示例的是,它在Person
和Employee
上使用相同的函数名,即getInfo
,并使用公共代码在两个对象上调用它们。这可以被称为多态性的一个例子,但可能是一个基本的例子这确实是多态性的一个弱示例,除了您已经注意到的问题(即
Employee.prototype
setup是superflous,示例中从未使用过prototype函数)之外,我还看到了一些其他问题:
Employee
函数(即构造函数)从不调用“基类”构造函数,即Person
函数-如果它们处于父子关系中,您可能会认为这是正确的Employee
函数从父函数粘贴了代码副本:
this.salary=salary;
这个。年龄=年龄;
这个。重量=重量代码>
getInfo
函数的Idem-两个构造函数都创建它们的特定函数并将其分配给此
;而getInfo
的Employee
版本从不调用基类版本——它可能应该调用基类版本来演示继承
因此,实际上,唯一有利于此示例的是,它在Person
和Employee
上使用相同的函数名,即getInfo
,并使用公共代码在两个对象上调用它们。这可以被称为多态性的一个例子,但可能是一个基本的例子var o = {
a: 2,
m: function(b){
return this.a + 1;
}
};
console.log(o.m()); // 3
// When calling o.m in this case, 'this' refers to o
var p = Object.create(o);
// p is an object that inherits from o
p.a = 4; // creates an own property 'a' on p
console.log(p.m()); // 5
// when p.m is called, 'this' refers to p.
// So when p inherits the function m of o,
// 'this.a' means p.a, the own property 'a' of p
您可以在MDN继承“方法”中找到您想要的内容
JavaScript没有基于类的语言定义的“方法”。在JavaScript中,任何函数都可以以属性的形式添加到对象中。继承函数的作用与任何其他属性一样,包括如上所示的属性阴影(在本例中,是方法重写的一种形式)
当执行继承的函数时,该值指向继承对象,而不是指向原型对象,其中函数是自己的属性
var o = {
a: 2,
m: function(b){
return this.a + 1;
}
};
console.log(o.m()); // 3
// When calling o.m in this case, 'this' refers to o
var p = Object.create(o);
// p is an object that inherits from o
p.a = 4; // creates an own property 'a' on p
console.log(p.m()); // 5
// when p.m is called, 'this' refers to p.
// So when p inherits the function m of o,
// 'this.a' means p.a, the own property 'a' of p
您可以在MDN中找到您想要的内容如果您可以添加一个提琴或提供一个似是而非的示例来学习JS中的多态性行为,那就太好了。请帮忙!有关基于原型的多态性的讨论,请参见。修复了上面文章中讨论的代码,这是多态性的一个工作示例。如果您可以添加一个fiddle或提供一个合理的示例来学习JS中的多态性行为,那将是非常棒的。请帮忙!有关基于原型的多态性的讨论,请参见。修复了上面文章中讨论的代码,是多态性的一个工作示例。