`
xiaoyongzeng
  • 浏览: 14504 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
JS加载顺序
   随着前台JS的模块化,动态的引用与加载外部JS是经常会遇到的一个问题,因为所引入的JS中往往有一些调用的依赖关系,如果处理不好,就引起了错误。为了保证a.js加载完了再加载b.js(这里暂且假设是b.js要调用a.js的函数或者实例等),必须有一个可靠的加载机制。
   自已也尝试写一个函数,但总觉得不够完美。也看了一些网上的有关此问题所给出的解决方案,觉得还是在开源网上这位仁兄给出的链式调用加载机制比较完美,现贴出如下:


代码:

var scriptLoader = {

	_loadScript : function(url, callback) {

		var head = document.getElementsByTagName('head')[0];

		var script = document.createElement('script');

		script.type = 'text/javascript';

		script.src = url;

		if (callback) {

			script.onreadystatechange = function() {

				if (this.readyState == 'loaded')
					callback();

			}

			script.onload = callback;

		}

		head.appendChild(script);

	},

	load : function(items, iteration) {

		if (!iteration)
			iteration = 0;

		if (items[iteration]) {

			scriptLoader._loadScript(

					items[iteration],

					function() {

						scriptLoader.load(items, iteration + 1);

					}

			)

		}

	}

}

用法:

scriptLoader.load(["a.js", "b.js"]);
JS继承
<script>

function dwn(s){
document.writeln(s);
}


var obj = {};
obj.extend = function(subClass,baseClass){
	function inheritance(){};  //继承的中间介子
	inheritance.prototype = baseClass.prototype;
	subClass.prototype = new inheritance(); //完成原型继承链
	subClass.prototype.constructor = subClass;
	subClass.baseConstructor = baseClass;
	subClass.superClass = baseClass.prototype;
	//alert("...");
	
}



function Person(name,age){
	this.name = name;
	this.age = age;
	this.array = new Array("1","2");
}



function Student(first,last,id){
	Student.baseConstructor.call(this,first,last);
	this.id = id;
}

obj.extend(Student,Person);
//alert("yyy");

var s = new Student("zs",22,1);
dwn(s);
dwn(s.id+",s.name: "+s.name);

dwn("s arry: "+s.array[0]);
s.array[0] = "er";

dwn("s arry: "+s.array[0]);

var s2 = new Student("ls",33,2);
dwn("s2.id: "+s2.id);
dwn("s2.array: "+s2.array[0]);






</script>
EXT事件内存泄漏与修复
==>EXT事件订阅由原来的on替换成mon,写法有点不一样
//Old 
this.el.on('click', this.onClick, this); 

//New  
this.mon(this.el, 'click', this.onClick, this);


==>其它事件自定义事件处理方式,尽量避免事件的匿名赋值方式


(function(){
    var o = document.getElementById("element");
    o.onclick = anotherObj;
})();

function anotherObj(){alert("....")};


window.onload=function(){
    var obj = document.getElementById("element");
    obj.onclick = doesNotLeak;
}
function doesNotLeak(){
    //Your Logic here
    alert("Hi! I have avoided the leak");
}
(转载)maximgb.tg.EditorGridPanel的NULL值问题
---NULL。在用这个控件,也遇到NULL值问题,参考下面的处理方式解决(来源于网上,具体不记得了)。
今天在使用Ext.ux.maximgb.tg.EditorGridPanel遇到展开记录的不会插入展开的节点中,而是直接插入到表格的末尾,经过无数次的尝试终于发现是JSON-lib不会输出null属性的原因,导致取根节点时_parent:null不会输入,把 
Java代码  
json.put("_parent", null);  

json.put("_parent", null);
改成Java代码  
json.put("_parent",JSONNull.getInstance() )  

json.put("_parent",JSONNull.getInstance() )
就可以了,也可以手动把_parent:null加上 
JS的toString与throw的用法举例。
<script>
 

 var AA = function(){
	this.toString = function(){
		return "[object AA]"; // 自定义实现TOSTRING方法。 测试时用于检查类型用。
	};

	this.test = function(_x){
		if(_x < 0){
			throw new Error("x must not be nagative..."); // THROW的用法,
		}
	}
 }
 var aa = new AA();

 try{
	aa.test(-1);
}catch(e){
	alert(e.message);  //捕获抛出的异常。如果不做任何处理,浏览器将会当做错误处理。
}


 

</script>
Global site tag (gtag.js) - Google Analytics