javascript已存在的对象构造器中是不能添加新的属性的:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<h2>JavaScript 对象</h2>

<p>你无法给构造函数添加新的属性。</p>

<p id="demo"></p>

<script>
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}

Person.nationality = "English";

var myFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sally", "Rally", 48, "green");

document.getElementById("demo").innerHTML =
"我父亲的国籍是 " + myFather.nationality;
</script>

</body>
</html>

posted @ 2019-07-04 14:54  技术颜良  阅读(566)  评论(0编辑  收藏  举报