国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript如何創(chuàng)建對象

2019-11-20 09:08:03
字體:
供稿:網(wǎng)友

JS是基于對象的語言,可以使用面向?qū)ο笏枷肽MJAVA|C++之類的面向?qū)ο笳Z言。
 •面向過程
◦關注解決問題的步驟

 •面向?qū)ο?
◦關注的是解決問題的所需要的對象(內(nèi)容和角色),然后根據(jù)業(yè)務邏輯按一定規(guī)則調(diào)用相關方法

對象分為系統(tǒng)對象和自定義對象兩種。我們可以通過調(diào)用系統(tǒng)構(gòu)造函數(shù)來創(chuàng)建出系統(tǒng)對象,如:array|date等。自定義對象必須自己創(chuàng)造,無法利用系統(tǒng)函數(shù)來創(chuàng)造。

javascript創(chuàng)建對象 

一、直接創(chuàng)建

 //直接創(chuàng)建     //JS創(chuàng)建對象     //1:創(chuàng)建空對象         var person1 = new Object();     //2:將該對象所需要的屬性、方法加進去         person1.name ="ailer" ;         console .log(person1.name);         person1.gender = "male";     //3:該對象添加的方法|(函數(shù))         person1. manager= function (){           console .log("Ailer is my English name" );        }     //4:調(diào)用對象方法:對象.方法名();         person1.manager();     //函數(shù)|方法?函數(shù)屬于對象時,該函數(shù)屬于這個對象下的方法;通過方法名來調(diào)用該函數(shù);     //變量|屬性 ?當變量屬于某一個對象時候, 該變量就是這個對象下的方法。通過屬性名來調(diào)用變量。      //增         person1.age ="6" ;      //改         person1.name ="lemon" ;      //查         console .log(person1.name);     //刪         delete person1.age;         console .log(person1.age);==> undefined      //引用類型,存儲的是地址      //基本類型:存儲的是值 標志位     /*  var arr1 = [1,2,3,4]        var arr2 = [5, 6, 7,9];        var arr2 = arr1;//        arr2[0]=10;//更改arr2里面的值,arr1也更改        alert(arr1[0]);//====>10 引用類型*/        var per2 = new Object();        per2.name = "Relia";        per2.age = "18";        per2.gender = "femal";        per2.hobby = "lemons";         //1:通過.(點語法)訪問屬性         //2:通過[](方括號)訪問對象的屬性;方括號中必須是屬性字符串或保存屬性字符串的變量|遍歷屬性的時候才使用方括號         var n = "name"         //console.log(per2["name"]);//雙引號         console .log(per2[n]);         for ( var property in per2) {//          console.log(per2[property]);         }

雖然直觀,但是創(chuàng)建多個對象的時候,代碼冗余

二、使用函數(shù)創(chuàng)建(工廠模式)

為了解決多個類似對象聲明的問題,我們可以使用一種叫做工廠模式的方法,這種方法就是為了解決實例化對象產(chǎn)生大量重復的問題。

//定義構(gòu)造函數(shù)       function createPerson ( name, age) {//創(chuàng)建一個新的空對象           var person = new Object;//添加屬性|方法           person.name = name;           person.age = age;           person. manager = function() {              console .log("ai" );           }//返回           return person;        }         var per1 = createPerson( "ailer", 12 );         console .log(per1.name);        var per2 = createPerson( "lemon", 23 );        console .log(per2.age);        console.log(per2 instanceof Object);//true        console.log(per2 instanceof createPerson);//false//無法區(qū)分該對象類型        console.log(per2.manager==per1.manager);//false 可得出per1和per2各自開閉空間

優(yōu):批量創(chuàng)建同類實例
缺:實例用同類屬性, 造成內(nèi)存浪費無法公,且無法區(qū)分該對象的類型

三、 字面量創(chuàng)建

優(yōu):簡單直接
缺:無法批量構(gòu)建同類對象

//字面量創(chuàng)建的對象使用constructor屬性不會指向?qū)嵗侵赶騩bject //使用字面量創(chuàng)建       var per1 = {        name:"Ailer",         constructor:per1,        age:12,        gender:"female",        hobby:"play",        eat:function(){          console.log(this.name);        }       }       per1.eat();//ailer       per1.name="lemon";       per1.eat();//lemon       console.log(typeof per1);//Object       console.log(per1.constructor==Object);//true

 四、new+構(gòu)造函數(shù)

//構(gòu)造函數(shù)創(chuàng)建對象,其子對象用instanceof不識別,所有采用new+obj創(chuàng)建        //對象識別了,但是仍然浪費一些代碼區(qū);==>產(chǎn)生原型創(chuàng)建        //創(chuàng)建js對象 new+構(gòu)造函數(shù)        //1:創(chuàng)建構(gòu)造函數(shù) |通常首字母大寫        function CreatePerson( name , age){          //2:把對象的屬性|方法掛靠在this指針身上, 當調(diào)用該函數(shù)創(chuàng)建對象時,this指針就指向這個新對象;          //這個this就添加給這個對象          this.name = name;          this.age = age;          /*this.speak = function(){            //此處this也指向創(chuàng)建對象            console.log(this.name+" hello");          }        }      /* CreatePerson.prototype.gender = "20";       CreatePerson.prototype. ea = function(){          console .log(this.name+ "sfd" );       }*///      __proto__:是:實例對象中的原型屬性, 指向?qū)獦?gòu)造函數(shù)對應的原型對象//      [[prototype]]        //調(diào)用        var per1 = new CreatePerson( "ailer", "20" );        var per2 = new CreatePerson( "relia", "18" );        console .log(per1 instanceof CreatePerson); //==true        console .log(per2 instanceof CreatePerson); //==>true        console .log(per1.speak== per2.speak); //==false說明系統(tǒng)開辟了兩個不同的代碼區(qū),造成了內(nèi)存浪費.

字面量創(chuàng)建一個比較方便,所以產(chǎn)生構(gòu)造函數(shù),普通構(gòu)造函數(shù)(工廠模式),子對象instanceof不識別且內(nèi)存浪費,用new+構(gòu)造函數(shù),子對象識別了,但仍有部分代碼重復,內(nèi)存浪費,產(chǎn)生原型代碼解決。

五、原型模式

      function CreateAnimal(name, age) {        //1.2:把外部參數(shù)綁定實例屬性        this.name = name;        this.age = age;      }      //1.3把相同的屬性,綁定在原型上(原型屬性,原型方法)      CreateAnimal.prototype.gender = "male";      CreateAnimal.prototype.style = function() {        console.log(this.name + " ailers");      };      //2:調(diào)用構(gòu)造函數(shù)創(chuàng)建對象      var cat1 = new CreateAnimal("xiaohua", "2");      var cat2 = new CreateAnimal("xiaohua", "2");      cat1.style();      console.log(cat1.style==cat2.style);//方法引用地址一樣,將屬性放到原型對象中,節(jié)約地址      //instanceof可以來判斷對象屬于哪一個【函數(shù)】      //constructor 建造者 也可以用來判斷對象屬于哪個【構(gòu)造函數(shù)】 【常】       //實例對象保存一個 constructor屬性指向它的構(gòu)造函數(shù)      //instanceof and constructor 區(qū)別      console.log(cat1 instanceof CreateAnimal);//true      console.log(cat1 instanceof Object);//true      console.log(cat1.constructor == CreateAnimal);//true      console.log(cat1.constructor == Object); //==false      //構(gòu)造函數(shù)的原型也有constructor屬性指會構(gòu)造函數(shù)      console.log(CreateAnimal.prototype.constructor == CreateAnimal);//true      //in判斷該屬性是否存在這個對象中,這個屬性為實例屬性或原型 //           alert("name" in cat1)//true//           alert("gender" in cat1);//true      //hasOwnProperty:來判斷某一個屬性到底是實例屬性,還是繼承自原型屬性 if 是 為true, else不存在|不是返回false;      console.log(cat1.hasOwnProperty("aaa"));//false 不存在的屬性返回為false      console.log(cat1.hasOwnProperty("name"));//true 實例屬性      console.log(cat1.hasOwnProperty("style"));//false 原型屬性返回為false      //遍歷屬性找原型屬性      //判斷參數(shù)是否為原型屬性  工具類      console.log(isPrototype("gender", cat1));//true      function isPrototype(proString, obj) {        if(proString in obj) {          if(!obj.hasOwnProperty(proString)) {            return true;          } else {            return false;          }        } else {          return false;        }      }      /*function isProperty(object, property) {//判斷原型中是否存在屬性   return !object.hasOwnProperty(property) && (property in object);}*/

動態(tài)原型模式

//構(gòu)造函數(shù)中初始化原型function per(name, age, gender) {        this.name = name;        this.age = age;        this.gender = gender;        //只在初始化原型的時候執(zhí)行一次        if(typeof this.sayName != "function") {          Person.prototype.sayName = function() {            alert(this.name);          }        }      }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 章丘市| 县级市| 大名县| 青田县| 外汇| 大英县| 日照市| 阿拉善盟| 平邑县| 长沙市| 邢台县| 绥中县| 长岛县| 清丰县| 荣昌县| 道孚县| 塔城市| 滨州市| 阜新| 长治县| 乌鲁木齐市| 龙江县| 融水| 拉孜县| 大方县| 宁德市| 松桃| 周至县| 齐齐哈尔市| 西丰县| 济源市| 聊城市| 大庆市| 长治县| 历史| 桂平市| 新沂市| 元谋县| 滕州市| 滕州市| 六枝特区|