js中面向对象的学习。
prototype,翻译为原型,我个人理解为原生态。可以用这样一个东西实现js的继承。
个人认为:如果定义了prototype,那么那个部分就相当于对象里的一员了,需要实例化才可以引用。所以也可以实现继承。
一、代码实现
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="js1.js"></script> </head> <body> </body> </html> |
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 |
/** * Created by Administrator on 2015/12/1. */ function a() { //这个东西也是可以继承的 this.sayhehe = function () { console.log("hehe"); } } // 成员方法 a.prototype.sayhello = function () { console.log("hello js"); } // 静态方法 a.sayhi = function () { console.log("i am godlikexie") } var so = new a(); // 这个成员方法只能用实例化的对象去调用 so.sayhello(); // 这个静态方法可以直接调用 a.sayhi(); function b() { } // b去继承a的方法 b.prototype = new a(); var b = new b(); b.sayhello(); b.sayhehe(); |
二、总结
总结一下,原生态的元素需要实例化才可以调用,静态的可以直接调用,没什么关系。