搞不懂得js,说明白了分全给了,越明白越好
在js的基础上封装的,common.js如下
var Tecent = Tecent || {};
Tecent.Common = Tecent.Common || ((function() {
var Common = {
extend: function(base, obj) {
for (var i in base) {
obj[i] = base[i];
}
return obj;
},
Inheritance: function(base, obj) {
for (var i in base.prototype) {
obj.prototype[i] = base.prototype[i];
}
return obj;
}
}
Common.extend({
Trim: function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
}
}, String.prototype);
window.Co = Common;
return Common;
})());
使用方法
Forms.Panel = Forms.Panel || ((function() {
var Panel = function(n, i) {
this.Init(n, i);
}
Tecent.Common.Inheritance(Forms.Base, Panel);//为什么要先这样继承,再接下来extend呢?
Co.extend({
Controls: null,
Init: function(n, i) {
this.Controls = [];
if (i == undefined) {
i = n;
this.Element = $('<span></span>');
}
else {
this.Element = $(n);
}
if (i) {
this.Show()
}
else {
this.Hide();
}
},
//添加一个控件到容器
Add: function(obj) {
if (obj.Element) {
this.Element.append(obj.Element);
this.Controls.push(obj);
}
else {
this.Element.append($(obj));
}
return this;
},
//删除容器//或容器内控件
Remove: M(function() {
this.Element.remove();
this.Controls = [];
}, function(obj) {
if (obj.Element) {
obj.Element.remove();
this.Controls.RemoveKey(obj);
}
else {
$(obj).remove();
}
})
}, Panel.prototype);
return Panel;
})());
1 为什么扩展和继承要这么写,可以这么写?而不是平时看书上说的,用call,或者apply去实现继承呢?extend是重写了jquery的方法吗?
extend: function(base, obj) {
for (var i in base) {
obj[i] = base[i];
}
return obj;
},
Inheritance: function(base, obj) {
for (var i in base.prototype) {
obj.prototype[i] = base.prototype[i];
}
return obj;
}
他们做了什么工作?为什么要这么做?
2 为什么panel的使用可以
var panel=new Forms.Panel()呢?
而var xyz = xyz || {};
xyz = xyz || (function() {
var refn = function(paramx,paramy) {
return paramx + paramy;
}
return refn;
} ());
debugger;
var thisxyz = new xyz();
这样却是错误的?其实这里错误我明白了,但刚才的panel明明定义后已经执行了,为什么还可以var panel=new Forms.Panel()呢?
var Tecent = Tecent || {};
Tecent.Common = Tecent.Common || ((function() {
var Common = {
extend: function(base, obj) {
for (var i in base) {
obj[i] = base[i];
}
return obj;
},
Inheritance: function(base, obj) {
for (var i in base.prototype) {
obj.prototype[i] = base.prototype[i];
}
return obj;
}
}
Common.extend({
Trim: function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
}
}, String.prototype);
window.Co = Common;
return Common;
})());
使用方法
Forms.Panel = Forms.Panel || ((function() {
var Panel = function(n, i) {
this.Init(n, i);
}
Tecent.Common.Inheritance(Forms.Base, Panel);//为什么要先这样继承,再接下来extend呢?
Co.extend({
Controls: null,
Init: function(n, i) {
this.Controls = [];
if (i == undefined) {
i = n;
this.Element = $('<span></span>');
}
else {
this.Element = $(n);
}
if (i) {
this.Show()
}
else {
this.Hide();
}
},
//添加一个控件到容器
Add: function(obj) {
if (obj.Element) {
this.Element.append(obj.Element);
this.Controls.push(obj);
}
else {
this.Element.append($(obj));
}
return this;
},
//删除容器//或容器内控件
Remove: M(function() {
this.Element.remove();
this.Controls = [];
}, function(obj) {
if (obj.Element) {
obj.Element.remove();
this.Controls.RemoveKey(obj);
}
else {
$(obj).remove();
}
})
}, Panel.prototype);
return Panel;
})());
1 为什么扩展和继承要这么写,可以这么写?而不是平时看书上说的,用call,或者apply去实现继承呢?extend是重写了jquery的方法吗?
extend: function(base, obj) {
for (var i in base) {
obj[i] = base[i];
}
return obj;
},
Inheritance: function(base, obj) {
for (var i in base.prototype) {
obj.prototype[i] = base.prototype[i];
}
return obj;
}
他们做了什么工作?为什么要这么做?
2 为什么panel的使用可以
var panel=new Forms.Panel()呢?
而var xyz = xyz || {};
xyz = xyz || (function() {
var refn = function(paramx,paramy) {
return paramx + paramy;
}
return refn;
} ());
debugger;
var thisxyz = new xyz();
这样却是错误的?其实这里错误我明白了,但刚才的panel明明定义后已经执行了,为什么还可以var panel=new Forms.Panel()呢?
...全文
相关推荐
JS实现CheckBox复选框全选、不选或全不选功能
CheckBox控件表明一个特定的状态(即选项)是选定 (on,值为1) 还是清除 (off,值为0)。在应用程序中使用该控件为用户提供“True/False”或“yes/no”的选择。因为 CheckBox 彼此独立工作,所以用户可以同时选择任意多个 CheckBox,进行选项组合。 CheckBox复选框JS实现全选、不选、全不选功能,很简单,具体内容如下 思路: 1、获取元素 2、给全选 不选 反选添加点击事件 3、用for循环checkbox 4、把checkbox的checked设置为true即实现全选 5、把checkbox的checked设置为false即实
Node.js | 从前端到全栈的必经之路
深入浅出Node.js,最适合前端开发人员进入全栈时学习的服务端语言,以JavaScript为基础,使前端人员能够平滑过渡到全栈,赶快来认识一下Node.js吧!
JavaScript案例-js全选和全不选.zip
JavaScript案例-js全选和全不选.zip
JS实现的全选、全不选及反选功能【案例】
主要介绍了JS实现的全选、全不选及反选功能,涉及javascript事件响应、元素遍历与属性动态操作相关实现技巧,需要的朋友可以参考下
用js代码写全选与全不选
js简单代码,最初级学员,高手就不用看啦!



