向JavaScript对象构造函数添加方法?
向对象构造函数 添加方法不同于向 普通对象添加方法。我们不能像普通对象那样添加方法。要在对象构造函数中创建方法,必须将其添加到对象构造函数中。
示例
在下面的示例中,该方法 被添加到构造函数中,因此,我们获得了合法的值。
<html>
<body>
<script>
function Business(name, property, age, designation) {
this.Name = name;
this.prop = property;
this.age = age;
this.designation = designation;
this.name = function() {
return this.Name
};
}
var person1 = new Business("Trump", "$28.05billion", "73", "President");
document.write(person1.name());
</script>
</body>
</html>输出结果
Trump
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短