先学习下new操作符吧 new关键字调用函数的心路历程: 1.创建一个新对象 2.将函数的作用域赋给新对象(this就指向这个对象) 3.执行函数中的代码 4.返回这个对象 根据这个的思路,来实现一个简单的new操作吧,代码演示: function myNew(Func, ...args) { if (typeof Func !== 'function') throw new Error(`${Func} is not a constructor`); const obj = Object.cr…
一.类的实现机制 在javascript中可以使用function关键字来定义一个类.在函数内通过this指针引用的变量或则方法都会成为类的成员. function classDemo(){ var $testProperty = "xz1024"; this.property = $testProperty; this.method = function(){ return "this is a test method"; } } var $obj = new c…