面向對象的概念:javascript
繼承的種種:html
``` /* 父類(基類) */ function Girl(name, age, sex) { this.name = name; this.age = age; this.sex = sex; this.hair = "長頭髮"; } Girl.prototype.chat = function () { console.log('hello world'); } /* 子類(派生類) */ function BeautifulGirl() { } // 把子類的原型設置爲父類的一個實例 BeautifulGirl.prototype = new Girl(); var bg1 = new BeautifulGirl(); console.log(bg1.hair); bg1.chat(); var bg2 = new BeautifulGirl(); console.log(bg2.hair); ```
``` /* 父類(基類) */ function Girl(name, age, sex, hair) { this.name = name; this.age = age; this.sex = sex; this.hair = hair; } Girl.prototype.chat = function () { console.log('hello world'); } /* 子類(派生類) */ function BeautifulGirl(name, age, sex, hair, longleg) { Girl.call(this, name, age, sex, hair); /* 冒充對象繼承 */ this.longleg = longleg; } var bg1 = new BeautifulGirl('真真', 18, '女', '大波浪', '大長腿'); console.log(bg1); ```
``` /* 父類(基類) */ function Girl(name, age, sex) { this.name = name; this.age = age; this.sex = sex; this.hair = "長頭髮"; this.ary = [1, 2, 3]; } Girl.prototype.chat = function () { console.log('hello world'); } /* 子類(派生類) */ function BeautifulGirl() { } BeautifulGirl.prototype = new Girl(); var bg1 = new BeautifulGirl(); console.log(bg1.hair); bg1.ary[1] = 5; bg1.chat(); var bg2 = new BeautifulGirl(); console.log(bg2.hair); console.log(bg2.ary); ```
``` var girl1 = { name: '如花', age: 18, sex: '女' ; var girl3 = new Object(); girl3.name = '春花'; girl3.age = 19; girl3.sex = '女'; ```
``` function girl(name, age, sex) { var obj = new Object(); /* 建立對象 */ obj.name = name; obj.age = age; obj.sex = sex; return obj; /* 返回對象 */ } var g1 = girl('如花', 18, '女'); console.log(g1); var g2 = girl('似玉', 18, '女'); console.log(g2); console.log(g1 instanceof girl); //false ```
- 1)-構造函數函數名首字母大寫(規律) - 2)-用new 方式執行,new操做符會自動建立並返回對象,這個對象稱之爲這個類的實例 - 3)-構造函數裏面的this指向當前實例 ``` function Girl(name, age, sex) { //自動建立對象 this.name = name; this.age = age; this.sex = sex; //自動返回對象 } var g1 = new Girl('如花', 18, '女'); var g2 = new Girl('似玉', 18, '女'); // Girl('如花', 18, '女'); //若是用普通函數方式執行,裏面this指向window // console.log(window); ```
- 全部對象天生自帶一個屬性 `__proto__`, 指向的是其構造函數的原型
- 全部的函數天生自帶一個屬性 `prototype`(原型)
``` function Girl(name, age, sex, str) { //自動建立對象 this.name = name; this.age = age; this.sex = sex; this.str = str; //自動返回對象 } Girl.prototype.chat = function () { // 公有方法裏面的this指向當前調用它的實例 console.log(this.str + 'hahaha'); } var g1 = new Girl('如花', 18, '女', 'hello world'); g1.chat(); ```
``` /* 動態混合模式 */ function Girl(name, age, str) { this.name = name; this.age = age; this.str = str; if (typeof chat != 'function') { //若是不存在chat方法,就添加 Girl.prototype.chat = function () { console.log(this.str); } } } var g1 = new Girl('小紅', 18, 'hello world'); ```
call和apply方法能夠改變this的指向——使用方法參考:java
var obj = { a: 10, b: 20 } function fn(c, d) { console.log(this.a + this.b + c + d); } //fn.call(obj, 30, 40); fn.apply(obj, [30, 40]); //apply的第二個參數是一個數組 fn.apply(null, [30, 40]); // 第一個參數傳null,指向window ```
call()方法和apply()方法用法總結 - 菲比月 - 博客園 https://www.cnblogs.com/phoebeyue/p/9216514.html編程