js幾種建立對象的方式

來源:互聯網
上載者:User

標籤:io   ar   sp   on   div   cti   bs   ef   new   

//1.0.1 定義js對象的方式1        function c01DefineObj1() {            var obj = new Object();            obj.Name = "八戒";            obj.Age = 500;            obj.Say = function () {                //alert("Name=" + obj.Name + ", Age=" + obj.Age);                alert("Name=" + this.Name + ", Age=" + this.Age);            }             //執行obj中的Say方法            obj.Say();        }    -----------------------------------------------------------------------//1.0.2 定義js對象的方式2,利用js對象的字面量表示形式        function DefineObj2() {            var obj2 =           {               Name: "唐僧",               Age: 1000,               Say: function () {                   alert("name=" + this.Name + " ,Age=" + this.Age);               }           };  ------------------------------------------------------------------------- //1.0 建立對象(建構函式執行個體化)        function Person(name, age) {            var c = new Object();            c.name = name;            c.age = age;            c.sayName = function () { alert(this.name); };            return c;        }        //直接new函數Person()執行方法體,最終建立好對象,將指標返回給p1        var p1 = new Person("ivan", 29);   -------------------------------------------------------------------------------------------------------  //2.0 建立對象(原型模式)        function dog(name, age) {            this.name = name;            this.age = age;            //this.SayHi = function () {            // alert("name=" + this.name + ",age=" + this.age);            //}        }        //給dog原型屬性上添加SayHi方法        dog.prototype.SayHi = function () {            alert("name=" + this.name + ",age=" + this.age);        }         var d1 = new dog("小黃", 2); ------------------------------------------------------------------------------------  //3.0 繼承(寄生組合模式)        function Pig(name, age) {            this.name = name;            this.age = age;            //this.SayHi = function () {            // alert("name=" + this.name + " ,age=" + this.age);            //};        }         Pig.prototype.SayHi = function () {            alert("name=" + this.name + " ,age=" + this.age);        };         function smallPig(name, age) {            //將Pig方法中的this改成smallPig中的this            Pig.call(this, name, age);        }        //將Pig上的原型繼承過來        smallPig.prototype = new Pig(); //將來在smallPig.prototype上添加新的屬性或者方法的時候才不會影響到Pig的原型        smallPig.prototype.SayHi1 = function () { };         var p12 = new smallPig("小豬", 1);        p12.SayHi(); -------------------------------------------------------------------------------------------------------------------------- //4.0 建立對象(原廠模式)        function createPerson(name, age) {            var c = new Object();            c.name = name;            c.age = age;            c.sayName = function () { alert(this.name); };            return c;        }         var p1 = createPerson("ivan", 29);        p1.sayName();        var p2 = createPerson("james", 30);        p2.sayName();     

js幾種建立對象的方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.