面向对象的概念: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编程