封装(概念) 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。封装是面向对象的特征之一,是对象和类概念的主要特性。 简单的说,一个类就是一个封装了数据以及操作这些数据的代码的逻辑实体。在一个对象内部,某些代码或某些数据可以是私有的,不能被外界访问。通过这种方式,对象对内部数据提供了不同级别的保护,以防止程序中无关的部分意外的改变或错误的使用了对象的私有部分。
继承(概念) 继承是指可以让某个类型的对象获得另一个类型的对象的属性的方法。它支持按级分类的概念。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。 通过继承创建的新类称为“子类”或“派生类”,被继承的类称为“基类”、“父类”或“超类”。继承的过程,就是从一般到特殊的过程。要实现继承,可以通过“继承”(Inheritance)和“组合”(Composition)来实现。继承概念的实现方式有二类:实现继承与接口继承。实现继承是指直接使用基类的属性和方法而无需额外编码的能力;接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力;
多态 多态就是指一个类实例的相同方法在不同情形有不同表现形式。多态机制使具有不同内部结构的对象可以共享相同的外部接口。这意味着,虽然针对不同对象的具体操作不同,但通过一个公共的类,它们(那些操作)可以通过相同的方式予以调用。
封装 es5封装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 function Person (name, sex, age ) { if (!(this instanceof Person )) { return new Person (name, sex, age); } this .name = name; this .sex = sex || 'female' ; this .age = age || 0 ; this .walk = function ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); } this .study = function (skill ) { console .log ('学习' + skill); } this .introduce = function ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } } var p = new Person ('小名' , 'male' , 10 );var p1 = Person ('小红' , 'female' , 9 );p.walk (); p1.study ('游泳' ); p.introduce (); p1.introduce ();
原型链的方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 function Person (name, sex, age ) { if (!(this instanceof Person )) { return new Person (name, sex, age); } this .name = name; this .sex = sex; this .age = age; } Person .prototype .walk = function ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); } Person .prototype .study = function (skill ) { console .log ('学习' + skill); } Person .prototype .introduce = function ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } var p = new Person ('小名' , 'male' , 10 );var p1 = Person ('小红' , 'female' , 9 );p.walk (); p1.study ('游泳' ); p.introduce (); p1.introduce ();
es6封装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class Person { constructor (name, sex, age ) { this .name = name; this .sex = sex; this .age = age; } walk ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); } study (skill ) { console .log ('学习' + skill); } introduce ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } } var p = new Person ('小名' , 'male' , 10 );p.walk (); p.introduce (); console .log (typeof Person );
继承 es5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 function Person (name, sex, age ) { if (!(this instanceof Person )) { return new Person (name, sex, age); } this .name = name; this .sex = sex || 'female' ; this .age = age || 0 ; this .walk = function ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); } this .study = function (skill ) { console .log ('学习' + skill); } this .introduce = function ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } } function Boy (name, age ) { this .name = name; this .age = age; this .sex = 'male' ; this .doHouseWork = function ( ) { console .log ('我在做家务' ); } } Boy .prototype = new Person ();Boy .prototype .constructor = Boy ;var boy = new Boy ('汤姆' , 12 );boy.introduce (); boy.doHouseWork (); console .log (boy instanceof Boy );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 function create (obj ) { return Object .create (obj); } var person = { name : 'Tom' , age : 20 , sex : 'male' , walk : function ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); }, study : function (skill ) { console .log ('学习' + skill); }, introduce : function ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } }; var boy = create (person);boy.age = 15 , boy.name = '晓东' ; boy.sex = 'male' ; boy.doHouseWork = function ( ) { console .log ('我在做家务' ); } boy.introduce (); boy.doHouseWork ();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 function Person (name, sex, age ) { if (!(this instanceof Person )) { return new Person (name, sex, age); } this .name = name; this .sex = sex || 'female' ; this .age = age || 0 ; this .walk = function ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); } this .study = function (skill ) { console .log ('学习' + skill); } this .introduce = function ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } } function Boy (name, age ) { var obj = Person .call (this , name, 'male' , age); obj.doHouseWork = function ( ) { console .log ('我在做家务' ); } return obj } let boy = Boy ('小米' , 16 );boy.introduce (boy); boy.doHouseWork ();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 function Person (name, sex, age ) { if (!(this instanceof Person )) { return new Person (name, sex, age); } this .name = name; this .sex = sex || 'female' ; this .age = age || 0 ; this .walk = function ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); } this .study = function (skill ) { console .log ('学习' + skill); } this .introduce = function ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } } function Boy (name, age ) { var obj = Person .apply (this , [name, 'male' , age]); obj.doHouseWork = function ( ) { console .log ('我在做家务' ); } return obj } let boy = Boy ('小米' , 17 );boy.introduce (boy); boy.doHouseWork ();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 class Person { constructor (name, sex, age ) { this .name = name; this .sex = sex; this .age = age; } walk ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); } study (skill ) { console .log ('学习' + skill); } introduce ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } } class Boy extends Person { constructor (name, age ) { super (name, 'male' , age); } doHouseWork ( ) { console .log ('我在做家务' ); } } var boy = new Boy ('汤姆' , 14 );boy.introduce (); boy.doHouseWork (); console .log (boy instanceof Boy );
多态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 function Person (name, sex, age ) { if (!(this instanceof Person )) { return new Person (name, sex, age); } this .name = name; this .sex = sex || 'female' ; this .age = age || 0 ; this .walk = function ( ) { if (this .age <= 2 ) { return console .log ('我不会走路' ); } if (this .age >2 && this .age < 4 ) { return console .log ('我会走路了' ); } return console .log ('走路' ); } this .study = function (skill ) { console .log ('学习' + skill); } this .introduce = function ( ) { console .log (`我是${this .name} ,我是一个${this .sex === 'male' ? "男" : "女" } 孩,今年${this .age} 岁了。` ); } } function Mathematician (name, age ) { this .sex = 'male' ; this .calc = function ( ) { var argsLength = arguments .length ; switch (argsLength) { case 1 : console .log ("这是" + arguments [0 ]); break ; case 2 : console .log (arguments [0 ] + '+' + arguments [1 ] + '=' + (arguments [0 ] + arguments [1 ])); break ; default : console .log ('太复杂的暂时不会' ); } } } Mathematician .prototype = new Person ();Mathematician .prototype .constructor = Mathematician ;var boy = new Mathematician ();boy.calc (); boy.calc (1 ); boy.calc (1 , 3 );