咱們在平常編程中,用到的大多都是js面向過程的編程,可是20%的編程咱們要運到面向對象,建立對象實例(類),下邊說一下,咱們建立對象的幾種方法!編程
第一個方法學習
//new Object var person=new Object person.name="xx"; person.age=xx; person.....
第二種方法this
//直接建立一個對象,字面量形式 var person={ name="xx", age="xx", ... } }
上邊的方法咱們常常用來學習,可是有一些詬病,若是你想要建立多個對象,能夠使用下邊這種方法。。prototype
介紹一下這種方法code
//建立對象實例 function Student(name,age,xxx){ this.name="", this.age="", this.xxx, } //下邊是建立對象的方法 //用到了對象的繼承 類 var student1 = new Student('xx', 19, 'xx'); var student2 = new Student('xx', 23, 'xx');
咱們用下邊這種方法若是建立10個對象是否是感受比上邊那個方便?對象
咱們要想對一個對象使用或者建立本身的方法就用到了原型對象prototype!接着上邊的繼承
student1.prototype.fly=function(){ console.log("我會飛") } student2.prototype.run=function(){ console.log("我會跑") }
這就是面向對象的一些基礎,若是你們以爲能夠的話,給個贊,有什麼問題也能夠在下邊評論,我會爲大家解答!原型