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
| <!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>class</title> </head> <body> <script> class rect{ constructor(w,h){ this.width = w; this.height = h; } c(){ return 2*(this.width+this.height); } s(){ return this.width*this.height; } } var r1 = new rect(10,20); console.log(r1.c(),r1.s());
class square extends rect{ constructor(w){ super(w,w); this.cname = "正方形" } } var s1 = new square(10); console.log(s1.c(),s1.s()); console.log(s1.hasOwnProperty("height")); console.log(s1.__proto__==square.prototype);
console.log(Object.getPrototypeOf("s1")==square.prototype); var s3 = {}; Object.setPrototypeOf(s3,s1);
console.log(s3.__proto__,s1); </script> </body> </html>
|