发布于 2016-12-16 12:38:55 | 167 次阅读 | 评论: 0 | 来源: 网友投递
这里有新鲜出炉的Javascript教程,程序狗速度看过来!
JavaScript客户端脚本语言
Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
function ShapeBase() {
this.show = function()
{
alert("ShapeBase show");
};
this.init = function(){
alert("ShapeBase init");
};
}
ShapeBase.prototype.show=function()
{
alert("ShapeBase show");
}
ShapeBase.prototype.init=function()
{
alert("ShapeBase init");
}
ShapeBase.prototype={
show:function()
{
alert("ShapeBase show");
},
init:function() {
alert("ShapeBase init");
}
};
function test(src){
var s=new ShapeBase();
s.init();
s.show();
}
//静态方法
ShapeBase.StaticDraw = function()
{
alert("method draw is static");
}
Object.extend = function(destination, source) {
for (property in source) {
destination[property] = source[property];
}
return destination;
}
Object.prototype.extend = function(object) {
return Object.extend.apply(this, [this, object]);
}
function Rect() {
}
Rect.prototype = ShapeBase.prototype; //只这一句就行了
//扩充新的方法
Rect.prototype.add=function() {
alert("Rect add");
}
ShapeBase.prototype={
show:function()
{
alert("ShapeBase show");
},
initialize:function () {
this.oninit();
}
};
Rect.prototype=(new ShapeBase).extend({
//添加新的方法
add:function() {
alert("Rect add");
},
//使用这种方法可以重写show方法
show:function() {
alert("Rect show");
},
//实现虚方法
oninit:function() {
alert("Rect oninit");
}
})
function test(src){
ShapeBase.StaticDraw();
var s=new ShapeBase();
s.show(); //alert("ShapeBase show")
var r=new Rect();
r.show(); //alert("Rect show")
r.add();
r.initialize(); //alert("Rect oninit")
}
//
//对象属性复制方法,很多库都有实现,如PrototypeJS里面的extend和Ext里面的Ext.apply
//
function extend(des, src) {
if (!des)
des = {};
if (src) {
for (var i in src) {
des[i] = src[i];
}
}
return des;
}
var CC = {}; //全局变量
//
//create 用于创建类
//
CC.create = function(superclass, constructor){
var clazz = (function() {
this.initialize.apply(this, arguments);
});
//如果无参数,直接返回类.
if(arguments.length == 0)
return clazz;
//如果无父类,此时constructor应该为一个纯对象,直接复制属性返回.
if(!superclass){
extend(clazz.prototype, constructor);
return clazz;
}
var absObj = clazz.prototype,
sprPropty = superclass.prototype;
if(sprPropty){
//用于访问父类方法
clazz.superclass = sprPropty;
extend(absObj, sprPropty);
//调用属性构造函数创建属性,这个是实现关键.
extend(absObj, constructor(sprPropty));
// 子类实例直接通过obj.superclass访问父类属性,
// 如果不想造成过多引用,也可把这句注释掉,因为多数时候是没必要的.
absObj.superclass = sprPropty;
//
clazz.constructor = constructor;
}
return clazz;
}
//
//创建一个动物类
//
var Animal = CC.create(null, {
//属性
footprint : '- - - - - - =',
//类初始化方法,必须的,当用 new 生成一个类时该方法自动被调用,参见上定义.
initialize : function(options){
extend(this, options);
alert('Animal initialize method is called.');
},
eat : function(){
alert('Animal eat method is called.');
},
move : function(){
alert('I am moving like this '+ this.footprint +' .');
}
});
//
//创建一个Duke类
//
var Duke = CC.create(Animal, function(superclass){
//在这可以定义一些类全局静态数据,该类每个实例都共享这些数据.
//计算实例个类,包括派生类实例.
var static_instance_counter = 0;
function classUtilityFuncHere(){ }
//返回类具体属性.
return {
//重写初始化方法
//@override
initialize : function(options) {
alert('Initializing Duke class..');
//调用父类初始化,这种方法比一般其它库的要简洁点吧,可以不管父类是什么.
superclass.initialize.call(this, options);
//做一些子类喜欢做的事.
alert('Duke initialize method is called.');
//读取或修改类静态属性
static_instance_counter++;
},
//重写move方法,增加Duke自己的移动方式.
move : function(){
this.footprint = this.footprint + 'zzzzzzzz';
superclass.move.call(this);
},
//重写eat方法,注意,里面不调用父类方法,即父类eat被覆盖了.
eat : function(){
alert('Duke is eating..');
},
//新增一个say方法,显示当前已经初始化的Duke类实例数量.
say : function(){
alert('the number of Duke instances is '+static_instance_counter);
}
};
});
var DukeChild = CC.create(Duke, function(superclass){
return {
move : function(){
this.footprint = this.footprint + '++++++++++++=';
superclass.move.call(this);
},
say : function(){
alert(this.msg || '');
}
};
});
(function test() {
var animal = new Animal();
animal.eat();
animal.move();
var dukeA = new Duke();
dukeA.eat();
dukeA.move();
dukeA.say();
var dukeB = new Duke();
dukeB.eat();
dukeB.move();
dukeB.say();
var dukeC = new DukeChild({msg : 'I am a child of duke.'});
dukeC.move();
dukeC.say();
})();