💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script type="text/javascript"> // 继承 var obj = { a:1, b:{ c:3 } } var obj1 = { d:4 } deepCopy(obj1, obj); function deepCopy(target, source){ for(var k in source){ if(typeof source[k] == 'object'){ if(Array.isArray(source[k])){ target[k] = []; }else{ target[k] = {}; } deepCopy(target[k], source[k]); }else{ target[k] = source[k]; } }; } </script> </body> </html> ``` ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script type="text/javascript"> // 继承 var obj = { a:1, b:{ c:3 } } var obj1 = { d:4 } // new函数生成的对象可以访问函数的prototype // 对象是可以访问他的__proto__ obj1.__proto__ = obj console.log(obj1); </script> </body> </html> ``` ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script type="text/javascript"> // 继承 var obj = { a:1, b:{ c:3 } } // Object.create就相当于方法2 __proto__,但是更加清晰明了 var obj1 = Object.create(obj); console.log(obj1); </script> </body> </html> ``` ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script type="text/javascript"> // 继承 function Animal(age, name) { this.type = '动物'; this.age = age; this.name = name; } Animal.prototype.getType = function (){ return this.type; } // 静态属性或者静态方法 Animal.x = 1; function Cat(age, name) { // 执行他父亲的构造函数,继承了他的一些属性 Animal.call(this, age, name); } Cat.prototype.say = function (){ console.log('喵喵!'); } // 利用原型继承父亲的一些方法和属性 // Cat.prototype.__proto__ = Animal.prototype; // 跟上面代码等价 Object.setPrototypeOf(Cat.prototype, Animal.prototype); // Cat.__proto__ = Animal; // 上下等价 Object.setPrototypeOf(Cat, Animal); var c1 = new Cat(7, '加菲猫'); console.log(c1); </script> </body> </html> ```