JS 添加到函数而不是替换为原型
JS add on to function instead of replace with prototype
base = {
init : function() {
console.log('initiate')
}
}
function Plugin(){
this.init = function() {
console.log('init me');
}
}
Plugin.prototype = base;
x = new Plugin();
x.init();
我希望它同时记录控制台日志,我希望能够像在基本变量中一样设置一组默认函数,并在函数插件中添加它们。
不确定这是否是最佳解决方案但已解决:
base = {
init : function() {
console.log('initiate')
}
}
function Plugin(){
this.init = function() {
Plugin.prototype.init.call(this);
console.log('init me');
}
}
Plugin.prototype = base;
x = new Plugin();
x.init();
另一个解决方案,也不确定这是否是最好的。
base = {
init : function() {
document.write(' initiate ')
}
}
function Plugin(){
this.init = function() {
this.__proto__.init ();
document.write(' init me ');
}
}
Plugin.prototype = base;
x = new Plugin();
x.init();
base = {
init : function() {
console.log('initiate')
}
}
function Plugin(){
this.init = function() {
console.log('init me');
}
}
Plugin.prototype = base;
x = new Plugin();
x.init();
我希望它同时记录控制台日志,我希望能够像在基本变量中一样设置一组默认函数,并在函数插件中添加它们。
不确定这是否是最佳解决方案但已解决:
base = {
init : function() {
console.log('initiate')
}
}
function Plugin(){
this.init = function() {
Plugin.prototype.init.call(this);
console.log('init me');
}
}
Plugin.prototype = base;
x = new Plugin();
x.init();
另一个解决方案,也不确定这是否是最好的。
base = {
init : function() {
document.write(' initiate ')
}
}
function Plugin(){
this.init = function() {
this.__proto__.init ();
document.write(' init me ');
}
}
Plugin.prototype = base;
x = new Plugin();
x.init();