向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 老公开业祝福语简短
18 官宣新年祝福语简短