var Class = function(){
var klass = function(){
this.init.apply(this, arguments);
};
klass.prototype.init = function(){};
return klass;
};
var Person = new Class;
Person.prototype.init = function(){ // 基于Person的实例做初始化};
// 用法:
var person = new Person
为什么Klass构造函数要调用apply方法
实例化Klass的时候this.init方法的this不就是指向person,为什么还要调用apply,
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
就你的目前的类结果看似没有必要,把
apply去掉,改成如果下this.init.apply(this, arguments);作用将传入构造函数的实际参数传入init方法中调用,执行你期望的初始化工作