实例
在本例中,我们将展示如何使用 prototype 属性来向对象添加属性:
<script type="text/javascript">function employee(name,job,born){this.name=name;this.job=job;this.born=born;}var bill=new employee("Bill Gates","Engineer",1985);employee.prototype.salary=null;
bill.salary=20000;document.write(bill.salary);</script>
输出:
20000
自定义类
function User(name,age){ this.name=name; this.age=age; console.debug(this);}User.prototype.showName=function(){ console.debug(this.name); }
var user=new User('armo',12);user.showName();
User('armo',12);//打印 window对象(函数由window对象调用)
new():
对象实例化
var person1 = new Person('Adam');
var person2 = new Person('Eve');
示例:
function Person(personName) {
this.info = 'I am called' + this.name;
this.infoShow = function () {
var person1 = new Person('Adam');
var person2 = new Person('Eve');
<input type="button" value="show info on Adam" onclick="person1.infoShow()"><br>
<input type="button" value="show info on Eve" onclick="person2.infoShow()">