prototype

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--使用 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;

    employee.prototype = {
        testProperty:"zhangjie"
    }/*这样做不会删除原来的属性,原因在于prototype只是用来添加属性的,并不会删除属性*/
    var newOne = new employee();
    document.write(bill.salary);/*正常显示2000*/
    document.write(bill.testProperty); /*undefined*/ /*testProperty属性是在bill之后才设定的,*/
    document.write(newOne.name);/*undefined未定义*/
    document.write(newOne.testProperty);/*zhangjie*/ /*正常显示*/
</script>
</body>
</html>

 

posted @ 2018-12-10 12:55  林丶  阅读(230)  评论(0编辑  收藏  举报