我试图在javascript中实现继承.我想出了以下最小代码来支持它.
function Base(){
this.call = function(handler, args){
handler.call(this, args);
}
}
Base.extend = function(child, parent){
parent.apply(child);
child.base = new parent;
child.base.child = child;
}
Run Code Online (Sandbox Code Playgroud)
专家,请告诉我这是否足够或我可能错过的任何其他重要问题.根据面临的类似问题,请提出其他更改建议.
这是完整的测试脚本:
function Base(){
this.call = function(handler, args){
handler.call(this, args);
}
this.superalert = function(){
alert('tst');
}
}
Base.extend = function(child, parent){
parent.apply(child);
child.base = new parent;
child.base.child = child;
}
function Child(){
Base.extend(this, Base);
this.width = 20;
this.height = 15;
this.a = ['s',''];
this.alert = function(){
alert(this.a.length);
alert(this.height);
}
}
function Child1(){
Base.extend(this, Child);
this.depth = …Run Code Online (Sandbox Code Playgroud) 我正在Marionette.View使用我命名的方法扩展Marionette的基本类型quickClick.我这样做
配置/木偶/ view.js
(function() {
define([
'backbone.marionette'
],
function(Marionette){
return _.extend(Backbone.Marionette.View.prototype, {
quickClick: function(e) {
$(e.target).get(0).click();
}
});
});
}).call(this);
Run Code Online (Sandbox Code Playgroud)
这允许我从我创建的任何视图中调用此方法,而无需为每个视图重新定义它.大!
这是一个精简的视图,事件对象仍然存在:
(function() {
define([
'backbone.marionette',
'app',
'templates'
],
function(Marionette, App, templates){
// Define our Sub Module under App
var List = App.module("SomeApp");
List.Lessons = Backbone.Marionette.ItemView.extend({
events: {
'tap .section-container p.title': 'quickClick'
}
});
// Return the module
return List;
});
}).call(this);
Run Code Online (Sandbox Code Playgroud)
万一你想知道,tap是我在Hammer.js使用的一个事件,因为这是一个移动应用程序.因此,为了规避iOS和Android上的300ms点击事件延迟,我会click在tap事件触发某些元素时手动触发事件.
现在,所有这些工作都很顺利,我觉得有必要对此进行详细描述,以便在上下文中给出答案.
我的问题是必须定义events对象.我完全不介意上面提到的那些元素,.section-container …