<html>
<head>
<script type="text/javascript">
/*
说明:在构造函数中通过标识量让所有对象共享一个方法,而每个
对象拥有自己的属性。
*/
function Person(username, password)
{
this.username = username;
this.password = password;
if(typeof Person.flag == "undefined")
{
Person.prototype.getInfo = function()
{
alert(this.username + ":" + this.password);
}
Person.flag = true;
}
}
var person1 = new Person("zhangsan", "0123");
var person2 = new Person("lisi", "4567");
person1.getInfo();
person2.getInfo();
</script>
</head>
<body>
</body>
</html>
JS中定义对象方式五: 使用动态原型方式创建对象
最新推荐文章于 2024-08-16 10:21:34 发布
本文介绍了一种在JavaScript中使用构造函数创建对象时,如何让所有实例共享一个方法的同时保持各自属性独立性的技巧。这种方法可以有效减少内存占用并提高代码复用率。
219

被折叠的 条评论
为什么被折叠?



