這裏是我的實現,想知道是否有任何改進,或分享你的想法。你如何在現場實現jquery oop
//* global variable *//
//I put all the variable into $('body');
$base_url = $('body').data('base_url');
$logged_in = $('body').data('logged_in');
//...
//* Object. like namespace *//
lzyy = {};
//* constructor *//
//correspond with mvc's c(controller)
lzyy.tag = function()
{
//new Class
m_ftl = new fav_tag_list('#fav_tag_list');
m_fti = new fav_tag_input('#fav_tag_input');
m_note = new note('#note');
};
//* method *//
//correspond with mvc's m(method)
lzyy.tag.property.add = function()
{
//* events bind *//
$('#fav_tag_btn').bind('click',function(e){
m_ftl.add_tag(m_fti.get_val());
m_fti.clear();
});
}
//* util functions *//
lzyy.preload_imgs = function()
{
for(var i = 0; i<arguments.length; i++)
{
jQuery("<img>").attr("src", arguments[i]);
}
};
lzyy.br2nl = function(str)
{
return str.replace(/<br\s*\/?>/mg,"\n");
};
//...
//* init *//
$(function()
{
//do something that can affect all pages
});
//* objects *//
//need another code snippet provided below
var Base_Class = Class.extend({
init:function(selecotr)
{
if(!this.self)
this.self = $(selecotr);
}
});
//for example,we have an dom element id = note
//we can add method , property
var note = Base_Class.extend({
close:function()
{
this.self.slideUp('fast');
}
});
//we can override parent method
var tag_input = Base_Class.extend({
init:function(selecotr)
{
this._super(selecotr);
if(this.self.length)
{
//do something...
}
},
append_item:function(data)
{
this.self.append(data);
}
});
//...
你知不知道有一種jQuery和JavaScript的區別?你正在談論JavaScript的BTW。 – Marius 2009-09-05 01:46:41