您的位置:首页 > Web前端 > JavaScript

js对象创建的方式及其优势和不足7

2014-11-13 23:24 267 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"

   "http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>js对象的创建7</title>

    <meta name="author" content="ASUS" />

    <!-- Date: 2014-11-13 -->

    <script type="text/javascript">

      function Person(name,age,friends){

         this.name = name;

         this.age = age;

         this.friends = friends;

        /*

          Person.prototype = {

                   constructor : Person,

                   sayHello : function(){

                   alert(this.name+","+this.age+",["+this.friends+"]");

                }*/

        

        //判断Person.prototype.sayHello是否已经存在 ,如何不存在就会创建,反之不创建

        if(!Person.prototype.sayHello){

              Person.prototype = {

           constructor : Person,

           sayHello : function(){

           alert(this.name+","+this.age+",["+this.friends+"]");

           }

        }

      }

     }

     

       var p1 = new Person("gui1",20,["aa","bb"]);       

       var p2 = new Person("gui2",22,["cc","dd"]);

       p2.sayHello();

      

        alert(p1.sayHello==p2.sayHello);//false

        alert(p1.prototype==p2.prototype);//true

    </script>

</head>

<body>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript js