<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> </style> </head> <body> <script type="text/javascript"> /*function Person(myName, myAge) { this.name=myName; this.age=myAge; this.hi=function() { console.log("h1"); } } /*Person.prototype.type="cyg"; Person.prototype.say=function() { console.log(this.name, this.age); }; */ //也可以这样写. /*Person.prototype={ constructor:Person, type:"cyg", say:function() { console.log(this.name, this.age); } };*/ class Person { constructor(myName, myAge) { this.name = myName; this.age = myAge; this.hi = function () { console.log("hi"); } } run() { console.log("run"); } } // Person.prototype.type = "人"; // Person.prototype.say = function () { // console.log(this.name, this.age); // }; let obj={constructor:Person,type:"cyg",say:function() { console.log(this.name, this.age); }}; Person.prototype=obj; let p = new Person("lnj", 34); console.log(p); </script> </body> </html>
JS中ES6类和对象
关注
打赏