作为一个好的脚手架使用

/*
Base.js, version 1.1a
Copyright 2006-2010, Dean Edwards
License: http://www.opensource.org/licenses/mit-license.php
*/ var Base = function() {
// dummy
}; Base.extend = function(_instance, _static) { // subclass
var extend = Base.prototype.extend; // build the prototype
Base._prototyping = true;
var proto = new this;
extend.call(proto, _instance);
proto.base = function() {
// call this method from any other method to invoke that method's ancestor
};
delete Base._prototyping; // create the wrapper for the constructor function
//var constructor = proto.constructor.valueOf(); //-dean
var constructor = proto.constructor;
var klass = proto.constructor = function() {
if (!Base._prototyping) {
if (this._constructing || this.constructor == klass) { // instantiation
this._constructing = true;
constructor.apply(this, arguments);
delete this._constructing;
} else if (arguments[0] != null) { // casting
return (arguments[0].extend || extend).call(arguments[0], proto);
}
}
}; // build the class interface
klass.ancestor = this;
klass.extend = this.extend;
klass.forEach = this.forEach;
klass.implement = this.implement;
klass.prototype = proto;
klass.toString = this.toString;
klass.valueOf = function(type) {
//return (type == "object") ? klass : constructor; //-dean
return (type == "object") ? klass : constructor.valueOf();
};
extend.call(klass, _static);
// class initialisation
if (typeof klass.init == "function") klass.init();
return klass;
}; Base.prototype = {
extend: function(source, value) {
if (arguments.length > 1) { // extending with a name/value pair
var ancestor = this[source];
if (ancestor && (typeof value == "function") && // overriding a method?
// the valueOf() comparison is to avoid circular references
(!ancestor.valueOf || ancestor.valueOf() != value.valueOf()) &&
/\bbase\b/.test(value)) {
// get the underlying method
var method = value.valueOf();
// override
value = function() {
var previous = this.base || Base.prototype.base;
this.base = ancestor;
var returnValue = method.apply(this, arguments);
this.base = previous;
return returnValue;
};
// point to the underlying method
value.valueOf = function(type) {
return (type == "object") ? value : method;
};
value.toString = Base.toString;
}
this[source] = value;
} else if (source) { // extending with an object literal
var extend = Base.prototype.extend;
// if this object has a customised extend method then use it
if (!Base._prototyping && typeof this != "function") {
extend = this.extend || extend;
}
var proto = {toSource: null};
// do the "toString" and other methods manually
var hidden = ["constructor", "toString", "valueOf"];
// if we are prototyping then include the constructor
var i = Base._prototyping ? 0 : 1;
while (key = hidden[i++]) {
if (source[key] != proto[key]) {
extend.call(this, key, source[key]); }
}
// copy each of the source object's properties to this object
for (var key in source) {
if (!proto[key]) extend.call(this, key, source[key]);
}
}
return this;
}
}; // initialise
Base = Base.extend({
constructor: function() {
this.extend(arguments[0]);
}
}, {
ancestor: Object,
version: "1.1", forEach: function(object, block, context) {
for (var key in object) {
if (this.prototype[key] === undefined) {
block.call(context, object[key], key, object);
}
}
}, implement: function() {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == "function") {
// if it's a function, call it
arguments[i](this.prototype);
} else {
// add the interface using the extend method
this.prototype.extend(arguments[i]);
}
}
return this;
}, toString: function() {
return String(this.valueOf());
}
});

【javascript】base.js的更多相关文章

  1. 【JavaScript】关于JS中的constructor与prototype

    最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...

  2. 【JavaScript】对JS的封装

    以下方法封装了获取ID元素的JS代码,调用以下代码方法并传值,则可以直接获取id所指元素 function $id(x) { //此处x只是形参,代表传进来的:要获取元素的id字符串 return d ...

  3. 【javascript】原生js更改css样式的两种方式

    下面我给大家介绍的是原生js更改CSS样式的两种方式: 1通过在javascript代码中的node.style.cssText="css表达式1:css表达式2:css表达式3  &quo ...

  4. 【JavaScript】初识js

    前端三大利器就是HTML+CSS+JavaScript,他们在整个前端开发中的主要作用大体可以概括如下 html 标记语言 负责页面的结构 css 层叠样式表 负责页面的样式 javascript 编 ...

  5. 【JavaScript】 knockout.js 日期格式化借助【momentjs】

    源:Knockout.js 日期格式化 源:momentjs

  6. 【JavaScript】用JS绘制一个球

    参考: 1.http://www.w3school.com.cn/html5/html_5_canvas.asp 2.http://blog.csdn.net/qq_27626333/article/ ...

  7. 【JavaScript】关于js的一些理解

    嵌套函数即作用域链:嵌套函数即闭包 函数表达式即延迟执行 匿名函数----------->实现块级作用域 call会切换到调用的对象参数环境.

  8. 【JavaScript】原生js实现:强制保留2位小数(由于toFixed()报错)

    function decimal(x) { var f = parseFloat(x); if (isNaN(f)) { alert("请输入数字!"); return; } va ...

  9. 【JavaScript】JS从入门到深入(复习查漏向

    [JavaScript]JS从入门到深入(复习查漏向 pre 精细得学过一遍JS后才发现,原来之前CTF中有些nodejs的题目以及一些游戏题的payload就变得很好理解了. 基础知识 ECMASc ...

随机推荐

  1. 【ACM】求高精度幂

    题目来源:http://poj.org/problem?id=1001&lang=zh-CN 求高精度幂 Time Limit: 500MS   Memory Limit: 10000K To ...

  2. springboot的controller使用及url参数的获取

    类上加上@RequestMapping其访问的地址就是类上的加上方法上的菜能访问到该方法,例如上图的地址就是/hello/say @RequestMapping(value = "/hell ...

  3. thinkphp getField("xxxxx", true); 得到一个字段所有值组成的的数组

    很多时候我们只需要一张表里某个字段的值,组成的数组 $Channel = D('channel');$channelList = $Channel->order('user_name')-> ...

  4. customs event

    // First create the event var myEvent = new CustomEvent("userLogin", { detail: { username: ...

  5. Android-----代码实现打开手机第三方应用APP

    最近做一个项目,有一个需要启动第三方应用,和微信的地图查看差不多,需要启动高德,百度或腾讯地图来查看:特来分享,希望有所帮助. 案例效果如图: 要想启动第三方:首先要知道他的包名 一:高德 高德:co ...

  6. 转:CentOS 7使用nmcli配置双网卡聚合LACP

    进入CentOS 7以后,网络方面变化比较大,例如eth0不见了,ifconfig不见了,其原因是网络服务全部都由NetworkManager管理了,下面记录下今天下午用nmcli配置的网卡聚合,网络 ...

  7. Day34 设计模式

    参考博客: http://www.cnblogs.com/alex3714/articles/5760582.html 什么是设计模式 Christopher Alexander:“每一个模式描述了一 ...

  8. Algorithm2: 重复查过半数的元素

    数组中,有一个元素的值在数组中重复的个数是超过一半,获得元素超过一半的元素值 int MoreThanHalfNumber(int * arr, int n){                  in ...

  9. 【51nod-1278】相离的圆(二分)

    思路 做法就是先把圆的直径化成线段,然后将线段的起点从小到大排序,以第i条线段为例,找i+1~n条中这样一条线段,满足是第一条且起点比第i条的终点要大(即满足相离),那么包括这条线段之后的线段也满足和 ...

  10. hdu 6053 TrickGCD(筛法+容斥)

    TrickGCD Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...