向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 周二祝福语大全简短
13 元旦祝福语简短有深度
14 小考加油文案祝福语简短
15 高温早安祝福语简短精辟
16 财运健康成语祝福语简短
17 出院祝福语简短8字
18 怎样给人送祝福语简短