javascript繼承的6種方法
1原型式繼承
簡介:對類式繼承的封裝,過渡對象至關於子類。javascript
- function inheritObject(o) {
-
- function F() {}
-
- F.prototype = o;
- return new F();
- }
- var book = {
- name : "javascript",
- book : ['js','css']
- };
- var newbook = inheritObject(book);
- newbook.name = "ajax";
- newbook.book.push("Node");
- var otherbook = inheritObject(book);
- otherbook.name = "xml";
- otherbook.book.push("React");
- console.log(newbook.name);
- console.log(newbook.book);
- console.log(otherbook.name);
- console.log(otherbook.book);
- console.log(book.name);
- console.log(book.book);
css
和類式繼承同樣,父類對象的引用類型值被共用。html
2.構造函數式繼承
- function SuperClass(id) {
- this.book = ['javascript','html','css'];
- this.id = id;
- }
- SuperClass.prototype.showBooks = function() {
- console.log(this.books);
- }
- function SubClass(id) {
-
- SuperClass.call(this,id);
- }
- var ins1 = new SubClass(1);
- var ins2 = new SubClass(2);
- ins1.book.push("Node");
- console.log(ins1.id);
- console.log(ins1.book);
- console.log(ins2.id);
- console.log(ins2.book);
-
- ins1.showBooks();
- function SuperClass(name) {
- this.name = name;
- this.book = ['javascript','html','css'];
- }
- SuperClass.prototype.getName = function () {
- console.log(this.name);
- };
- function SubClass(name,time) {
-
- SuperClass.call(this,name);
- this.time = time;
- }
- SubClass.prototype = new SuperClass();
- SubClass.prototype.getTime = function () {
- console.log(this.time);
- };
- var ins1 = new SubClass('Node',2016);
- ins1.book.push("Node");
- console.log(ins1.book);
- ins1.getName();
- ins1.getTime();
-
- var ins2 = new SubClass('React',2015);
- console.log(ins2.book);
- ins2.getName();
- ins2.getTime();
4.原型式繼承
- function inheritObject(o) {
-
- function F() {}
-
- F.prototype = o;
- return new F();
- }
- var book = {
- name : "javascript",
- book : ['js','css']
- };
- var newbook = inheritObject(book);
- newbook.name = "ajax";
- newbook.book.push("Node");
- var otherbook = inheritObject(book);
- otherbook.name = "xml";
- otherbook.book.push("React");
- console.log(newbook.name);
- console.log(newbook.book);
- console.log(otherbook.name);
- console.log(otherbook.book);
- console.log(book.name);
- console.log(book.book);
5.寄生式繼承
- function inheritObject(o) {
-
- function F() {}
-
- F.prototype = o;
- return new F();
- }
- var book = {
- name : "javascript",
- book : ['js','css']
- };
- function createBook(obj) {
-
- var o = new inheritObject(obj);
-
- o.getName = function() {
- console.log(name);
- }
-
- return o;
- }
-
- var newbook = createBook(book);
- newbook.name = "ajax";
- newbook.book.push("Node");
- var otherbook = createBook(book);
- otherbook.name = "xml";
- otherbook.book.push("React");
- console.log(newbook.name);
- console.log(newbook.book);
- console.log(otherbook.name);
- console.log(otherbook.book);
- console.log(book.name);
- console.log(book.book);
java
- function inheritObject(o) {
-
- function F() {}
-
- F.prototype = o;
- return new F();
- }
- function inheritPrototype(subClass,superClass) {
-
- var p = inheritObject(superClass.prototype);
-
- p.constructor = subClass;
-
- subClass.prototype = p;
- }
- function SuperClass(name) {
- this.name = name;
- this.colors = ["red","blue","green"];
- }
- SuperClass.prototype.getName = function() {
- console.log(this.name);
- }
- function SubClass(name,time) {
- SuperClass.call(this,name);
- this.time = time;
- }
- inheritPrototype(SubClass,SuperClass);
- SubClass.prototype.getTime = function() {
- console.log(this.time);
- }
- var ins1 = new SubClass("js",2014);
- var ins2 = new SubClass("css",2015);
- ins1.colors.push("black");
- console.log(ins1.colors);
- console.log(ins2.colors);
- ins2.getName();
- ins2.getTime();