今日学习笔记:深拷贝和浅拷贝2

it2025-08-10  9

老师和学生

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script type="text/javascript"> function Teacher(){ this.name = 'Mr.Li'; this.tSkill = 'JAVA'; } Teacher.prototype = { pSkill: 'JS/JQ' } var t = new Teacher(); console.log(t); function Student(){ this.name = 'Mr.Wang'; this.pSkill = 'HTML/CSS'; } function Buffer(){} Buffer.prototype = Teacher.prototype; var buffer = new Buffer(); Student.prototype = buffer; Student.prototype.age = 18; var student = new Student(); console.log(student); console.log(buffer); </script> </body> </html>

张三和李四

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script type="text/javascript"> //拷贝/复制/克隆 clone var person1 = { name: '张三', age: 18, sex: 'male', height: 180, weight: 140, son: { first: 'Jenney', second: 'Lucy', Third: 'Jone' } } console.log(person1); function Car(){ } var car = new Car(); console.log(car); var person2 = {}; console.log(person1['name']); console.log(person1.name); for(var key in person1){ person2[key] = person1[key]; } person2.name = '李四'; console.log(person1,person2); </script> </body> </html>

Mr.Zhang

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script type="text/javascript"> var inherit = (function(){ var Buffer = function(){} return function(Target, Origin){ Buffer.prototype = Origin.prototype; Target.prototype = new Buffer(); Buffer.prototype.constructor = Target; Buffer.prototype.super_class = Origin; } })(); Teacher.prototype.name = 'Mr.Zhang'; function Teacher(){} function Student(){} function Buffer(){} inherit(Student,Teacher); Student.prototype.age = 18; var s = new Student(); var t = new Teacher(); console.log(s); console.log(t); </script> </body> </html>
最新回复(0)