论坛首页 Web前端技术论坛

ECMAScript5 新特性(一)

浏览 8587 次
精华帖 (1) :: 良好帖 (4) :: 新手帖 (12) :: 隐藏帖 (1)
作者 正文
   发表时间:2011-01-24  
即使是ie6,很多html5的功能还是可以用的
0 请登录后投票
   发表时间:2011-01-25  
这个 ECMAScript5 在09年12月份就发布, 怎么给弄成10年12月了?
0 请登录后投票
   发表时间:2011-01-25  
现在项目在需求阶段就应该向客户指明:IE7以上版本。。。。我们就是这么搞的
0 请登录后投票
   发表时间:2011-01-25  
还是不爽,如果能这么些写就好了
var objDefinition = { "test" :  function(){} };var instance = new objDefinition();
0 请登录后投票
   发表时间:2011-01-25  

我靠 知音啊
0 请登录后投票
   发表时间:2011-01-25  
rainsilence 写道
即使是ie6,很多html5的功能还是可以用的

----------
我倒觉得完全不可用,倒是好事。早点淘汰。
0 请登录后投票
   发表时间:2011-01-25  
rainsilence 写道
即使是ie6,很多html5的功能还是可以用的

VML  而已
0 请登录后投票
   发表时间:2011-01-25  
能实现功能就行
0 请登录后投票
   发表时间:2011-01-28  

改自OpenLayers框架...
Class = function() {
	var len = arguments.length;
	var P = arguments[0];
	var F = arguments[len - 1];

	var C = typeof F.initialize == "function" ? F.initialize : function() {
		P.apply(this, arguments);
	};
	if (len > 1) {
		var newArgs = [ C, P ].concat(Array.prototype.slice.call(arguments)
				.slice(1, len - 1), F);
		inherit.apply(null, newArgs);
	} else {
		C.prototype = F;
		C.prototype.superclass = [];
	}
	if(C.prototype.tsuperclass){
		C.prototype.superclass = C.prototype.tsuperclass;
		delete C.prototype.tsuperclass;
	}
	return C;
}

 

 

inherit = function(C, P) {
	var F = function() {
	};
	F.prototype = P.prototype;
	C.prototype = new F;
	C.prototype.tsuperclass = [];
	addUnique(C.prototype.tsuperclass, P);
	var i, l, o;
	for (i = 2, l = arguments.length; i < l; i++) {
		o = arguments[i];
		//strict equal
		if (typeof o === "function") {
			addUnique(C.prototype.tsuperclass, o);
			o = o.prototype;
		}
		extend(C.prototype, o);
	}
};

 

 

extend = function(destination, source) {
    destination = destination || {};
    if(source) {
        for(var property in source) {
            var value = source[property];
            if(value !== undefined) {
                destination[property] = value;
            }
        }

        /**
         * IE doesn't include the toString property when iterating over an object's
         * properties with the for(property in object) syntax.  Explicitly check if
         * the source has its own toString property.
         */

        /*
         * FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative
         * prototype object" when calling hawOwnProperty if the source object
         * is an instance of window.Event.
         */

        var sourceIsEvt = typeof window.Event == "function"
                          && source instanceof window.Event;

        if(!sourceIsEvt
           && source.hasOwnProperty && source.hasOwnProperty('toString')) {
            destination.toString = source.toString;
        }
    }
    return destination;
};

 

 

addUnique = function(current, obj){
	if(!current||!obj){
		return;
	}	
	if(Kay.Util.indexOf(current, obj) != -1){
		return;
	}
	current.push(obj);
};

 

有了以上四个方法, 就可以这样定义类了


    var Dog = Class({
    name:null,
    bite:function(another){
        console.debug(this.name + ' bite ' + another.name + ' !');
    },
    getFeatures:function(){
        return '两只眼睛, 四只脚, 一条尾巴, 一张嘴';
    },
    initialize:function(options){
        extendz(options, this);
        //init 
    }
});

var JAPADog = Class(Dog, {
    from:'鸡国',
    getFeatures:function(){
        return Dog.prototype.apply(this, arguments) + (this.from + '的');
    }
});

var doga = new Dog({name:'小犬惷一郞'});
var dogb = new JAPADog({name:'小犬笨二郞'});
 doga.bite(dogb);
0 请登录后投票
   发表时间:2011-01-28  
adaikiss 写道

 

改自OpenLayers框架...
Class = function() {
	var len = arguments.length;
	var P = arguments[0];
	var F = arguments[len - 1];

	var C = typeof F.initialize == "function" ? F.initialize : function() {
		P.apply(this, arguments);
	};
	if (len > 1) {
		var newArgs = [ C, P ].concat(Array.prototype.slice.call(arguments)
				.slice(1, len - 1), F);
		inherit.apply(null, newArgs);
	} else {
		C.prototype = F;
		C.prototype.superclass = [];
	}
	if(C.prototype.tsuperclass){
		C.prototype.superclass = C.prototype.tsuperclass;
		delete C.prototype.tsuperclass;
	}
	return C;
}

 

 

 

inherit = function(C, P) {
	var F = function() {
	};
	F.prototype = P.prototype;
	C.prototype = new F;
	C.prototype.tsuperclass = [];
	addUnique(C.prototype.tsuperclass, P);
	var i, l, o;
	for (i = 2, l = arguments.length; i < l; i++) {
		o = arguments[i];
		//strict equal
		if (typeof o === "function") {
			addUnique(C.prototype.tsuperclass, o);
			o = o.prototype;
		}
		extend(C.prototype, o);
	}
};

 

 

 

extend = function(destination, source) {
    destination = destination || {};
    if(source) {
        for(var property in source) {
            var value = source[property];
            if(value !== undefined) {
                destination[property] = value;
            }
        }

        /**
         * IE doesn't include the toString property when iterating over an object's
         * properties with the for(property in object) syntax.  Explicitly check if
         * the source has its own toString property.
         */

        /*
         * FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative
         * prototype object" when calling hawOwnProperty if the source object
         * is an instance of window.Event.
         */

        var sourceIsEvt = typeof window.Event == "function"
                          && source instanceof window.Event;

        if(!sourceIsEvt
           && source.hasOwnProperty && source.hasOwnProperty('toString')) {
            destination.toString = source.toString;
        }
    }
    return destination;
};

 

 

 

addUnique = function(current, obj){
	if(!current||!obj){
		return;
	}	
	if(Kay.Util.indexOf(current, obj) != -1){
		return;
	}
	current.push(obj);
};

 

有了以上四个方法, 就可以这样定义类了

 


    var Dog = Class({
    name:null,
    bite:function(another){
        console.debug(this.name + ' bite ' + another.name + ' !');
    },
    getFeatures:function(){
        return '两只眼睛, 四只脚, 一条尾巴, 一张嘴';
    },
    initialize:function(options){
        extendz(options, this);
        //init 
    }
});

var JAPADog = Class(Dog, {
    from:'鸡国',
    getFeatures:function(){
        return Dog.prototype.apply(this, arguments) + (this.from + '的');
    }
});

var doga = new Dog({name:'小犬惷一郞'});
var dogb = new JAPADog({name:'小犬笨二郞'});
 doga.bite(dogb);

 

A!B!C!openlayer会把代码改的这么恶心??

 

另外,如果要实现继承,向上转型判断,以及自动调用父类函数请参照我的其他博客,比如:

 

 

 

0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics