// Create a new instance using f's prototype.
var newInstance = Object.create(f.prototype), result;
// Call the function
result = f.call(newInstance, a, b, c),
// If the result is a non-null object, use it, otherwise use the new instance.
result && typeof result === 'object' ? result : newInstance
最简单的,对象字面量啊~
你是说实例化一个类吧,对于用户自定义的类,
相当于
答案摘自这里
比如有个函数
不使用构造函数的话,可以这样:
不过有个注意的地方:
Object.create不会调用构造函数,所以name是undefined。
楼主问的是那个吗,简单的JavaScript继承法什么的?(希望我没记错名字)
我拷了一份过来。John Resig's Simple Javascript Inheritance
这个用起来就是XXX.extend()来继承出新的类(所有的类都继承自Class),然后用XXX.Create()来实例化了。
惯用Typescript的表示这些东西已经忘得差不多了。强烈推荐Typescript……反正ES6是趋势,早点享受上它的福利不也挺好的……