这段时间正好公司项目需要,需要修改fullcalendar日历插件,有机会深入插件源码。正好利用这个机会,我也大致学习了以下JS的面向对象编程,感觉收获还是比较多的。
所以写了下面这篇文章希望跟大家探讨探讨JS的面向对象,本人资历尚浅,还望各位大神多多指教。
总述:
$(selector).each(function(index,element)) 或 $.each(selector,function(index,element))
var arr1 = [ "aaa", "bbb", "ccc" ];
$.each(arr1, function(i,val){
alert(i); //输出: 0,1,2
alert(val); //输出:aaa,bbb,ccc
});
参数selector可以是dom元素( $("input:hidden") ),json( { one: 1 , two: 2 , three: 3 } )等等
function (object, callback, args) {
//该方法有三个参数:进行操作的对象obj,进行操作的函数fn,函数的参数args
var name, i = 0,length = object.length;
if (args) {
if (length == undefined) {
for (name in object) {
if (callback.apply(object[name], args) === false) {
break;
}
}
} else {
for (; i < length;) {
if (callback.apply(object[i++], args) === false) {
break;
}
}
}
} else {
if (length == undefined) {
for (name in object) {
if (callback.call(object[name], name, object[name]) === false) {
break;
}
}
} else {
for (var value = object[0]; i < length && !== false; value = object[++i]) {}
/*object[0]取得jQuery对象中的第一个DOM元素,通过for循环,
得到遍历整个jQuery对象中对应的每个DOM元素,通过 callback.call( value,i,value);
将callback的this对象指向value对象,并且传递两个参数,i表示索引值,value表示DOM元素;
其中callback是类似于 function(index, elem) { ... } 的方法。
所以就得到 $("...").each(function(index, elem){ ... });
*/
}
}
return object;
}
来源: http://www.cnblogs.com/xiaojinhe2/archive/2011/10/12/2208740.html
function segmentElementEach(segments, callback) { // TODO: use in AgendaView?
for (var i=0; i
// report the elements to the View, for general drag/resize utilities
segmentElementEach(segments, function(segment, element) {
reportEventElement(segment.event, element);
});
// attach mouse handlers
//attachHandlers(segments, modifiedEventId);
// call `eventAfterRender` callback for each event
segmentElementEach(segments, function(segment, element) {
trigger('eventAfterRender', segment.event, segment.event, element);
});
// the container where the initial HTML will be rendered.
// If `doAppend`==true, uses a temporary container.
var renderContainer = doAppend ? $("") : finalContainer;
数据类型 | 转为true的值 | 转为false的值 |
Boolean | true | false |
String | 任何非空字符串 | “”空字符串 |
Number | 任何非零数值 | 0和NaN |
Object | 任何对象 | null |
Undefined | n/a | undefined |
<script type="text/javascript">
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
alert(object.getNameFunc()()); //"The Window"
script>
//一个简单的用来发送消息的函数
function sendMessage( msg, obj ) {
//如果同时提供了一个消息和一个对象
if ( arguments.length == 2 )
//就将消息发给该对象
obj.handleMsg( msg );
else //否则,刚假定只有消息被提供
//于是显示该消息
alert( msg );
}
//用来延迟显示消息的通用函数
function delayedAlert( msg, time ) {
//初始化一个被封套的函数
setTimeout(function(){
//此函数使用了来自封套它的函数的变量msg
alert( msg );
}, time );
}
其实本人感觉,这好像并没体现出闭包的什么特点,倒反而像是简单的封装var datamodel = {
table : [],
tree : {}
};
(function(dm){
for(var i = 0; i < dm.table.rows; i++){
var row = dm.table.rows[i];
for(var j = 0; j < row.cells; i++){
drawCell(i, j);
}
}
//build dm.tree
})(datamodel);
var single = (function(){
var unique;
function Construct(){
// ... 生成单例的构造函数的代码
}
unique = new Constuct();
return unique;
})();
来源: <http://blog.chinaunix.net/uid-26672038-id-3886959.html>
function Person(){
var name = "default";
return {
getName : function(){
return name;
},
setName : function(newName){
name = newName;
}
}
};
var john = Person();
print(john.getName());
john.setName("john");
print(john.getName());
var jack = Person();
print(jack.getName());
jack.setName("jack");
print(jack.getName());
运行结果如下:
default
john
default
jack
//id为"main"的一个元素
var obj = document.getElementById("main");
//用来绑定的items数组
var items = [ "click", "keypress" ];
//遍历items中的每一项
for ( var i = 0; i < items.length; i++ ) {
//用自执行的匿名函数来激发作用域
(function(){
//在些作用域内存储值
var item = items[i];
//为obj元素绑定函数
obj[ "on" + item ] = function() {
//item引用一个父级的变量,
//该变量在此for循环的上文中已被成功地scoped(?)
alert( "Thanks for your " + item );
};
})();
}
function Person(name){
var name;
function moveUp(){
alert(name+" move");
}
this.moveUp = moveUp;
}
var jack = new Person("jack");
jack.moveUp();
function createPerson(name, age, job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
alert(this.name);
};
return o;
}
var person1 = createPerson("Nicholas", 29, "Software Engineer");
var person2 = createPerson("Greg", 27, "Doctor");
person1.sayName(); //"Nicholas"
person2.sayName(); //"Greg"
来源:
《Javascript 高级程序设计》 function Person(name){
var name;
function moveUp(){
alert(name+" move");
}
this.moveUp = moveUp;
}
var jack = new Person("jack");
jack.moveUp();
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
//inherit from SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function (){
return this.subproperty;
};
var instance = new SubType();
alert(instance.getSuperValue()); //true
//以下是fullcalendar插件中穿件日历基础对象时继承事件处理类型
EventManager的过程,t是用来存储this变量的,防止 // Imports
// -----------------------------------------------------------------------------------
EventManager.call(t, options);
var isFetchNeeded = t.isFetchNeeded;
var fetchEvents = t.fetchEvents;