javascript中面向对象的两种构建方式(构造函数)和(原型模式的区别)

1、构造函数模式---》alert的结果为false

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
        function Person(name,age,job){
            this.name = name;
            this.age = age;
            this.job = job;
            this.showname = function(){
                alert(this.name);
            }
            
            this.showage = function(){
                alert(this.age);
            }

            this.showjob = function(){
                alert(this.job);
            }
        }
        var tom = new Person('tom',18,'程序员');
        var jack = new Person('jack',19,'销售');
        alert(tom.showjob == jack.showjob);  //结果为False
    </script>
</head>
<body>    
</body>
</html>

 2.原型模式---->alert结果为true

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
        function Person(name,age,job){
            this.name = name;
            this.age = age;
            this.job = job;

        }
        Person.prototype.showname = function(){
            alert('我的名字叫'+this.name);
        }
        Person.prototype.showage = function(){
            alert(this.age);
        }
        Person.prototype.showjob = function(){
            alert(this.job);
        }

        var tom = new Person('tom',18,'程序员');
        var jack = new Person('jack',19,'销售');
        alert(tom.showjob == jack.showjob);//结果为True       
    </script>
</head>
<body>
    
</body>
</html>

 

posted @ 2018-05-08 11:18  猴子吃果冻  阅读(438)  评论(0编辑  收藏  举报