Posted on 2007-09-14 08:35
puras 阅读(1701)
评论(0) 编辑 收藏 所属分类:
JavaScript
作者:赫连紫軒(puras)
最近在看ExtJS,jQuery这些JS的框架
发现里面有很多基础的东西居然都不明白
无奈只能找本书复习复习基础啦
随手就记了点
- 共有5种原始类型:Undefined, Null, Boolean, Number和String
- 使用typeof运算符来判断一个值的类型
- 值null和值undefined是相等的
- NaN不能用于计算,并与自身不相等
- 位运算NOT实质上是对数字求负,然后减1,因此,25变为-26
- 如果函数无明确的返回值,或调用了没有参数的return语句,那么它真正返回的值是undefined
定义类或对象的方式
工厂方式:
function showColor() {
alert(this.color);
}
function createCar(sColor, iDoors, iMpg) {
var oTempCar = {};
oTempCar.color = sColor;
oTempCar.doors = iDoors;
oTempCar.mpg = iMpg;
oTempCar.showColor = showColor;
return oTempCar;
}
var oCar1 = createCar('red', 4, 23);
var oCar2 = createCar('blue', 2, 25);
构造函数方式:
function showColor() {
alert(this.color);
}
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.showColor = showColor;
}
var oCar1 = new Car('red', 4, 23);
var oCar2 = new Car('blue', 2, 25);
原型方式:
function Car() {}
Car.prototype.color = 'red';
Car.prototype.doors = 4;
Car.prototype.mpg = 23;
Car.prototype.showColor = function() {
alert(this.color);
};
var oCar1 = new Car();
var oCar2 = new Car();
混合的构造函数/原型方式(推荐使用):
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
}
Car.prototype.showColor = function() {
alert(this.color);
};
var oCar1 = new Car('red', 4, 23);
var oCar2 = new Car('blue', 2, 25);
动态原型方法:
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
if (typeof Car._initialized == 'undefined') {
Car.prototype.showColor = function() {
alert(this.color);
}
Car._initialized = true;
}
}
混合工厂方法:
function Car() {
var oTempCar = new Object();
oTempCar.color = 'red';
oTempCar.doors = 4;
oTempCar.mpg = 23;
oTempCar.showColor = function() {
alert(this.color);
};
return oTempCar;
}
var car = new Car();
继承的方式
对象冒充:
function ClassA(sColor) {
this.color = sColor;
this.sayColor = function() {
alert(this.color);
}
}
function ClassB(sColor, sName) {
this.newMethod = ClassA;
this.newMethod(sColor);
delete this.newMethod;
this.name = sName;
this.sayName = function() {
alert(this.name);
}
}
子类的新属性和方法必须在删除父类之后定义
call()方法:
function ClassA(sColor) {
this.color = sColor;
this.sayColor = function() {
alert(this.color);
}
}
function ClassB(sColor, sName) {
ClassA.call(this, sColor);
this.name = sName;
this.sayName = function() {
alert(this.name);
}
}
apply()方法:
function ClassA(sColor) {
this.color = sColor;
this.sayColor = function() {
alert(this.color);
}
}
function ClassB(sColor, sName) {
ClassA.call(this, new Array(sColor));
//ClassA.call(this, arguments); //当父类和子类的参数顺序完全一致时可使用此方法
this.name = sName;
this.sayName = function() {
alert(this.name);
}
}
原型链:
function ClassA() {
}
ClassA.prototype.color='red';
ClassA.prototype.sayColor = function() {
alert(this.color);
};
function ClassB() {
}
ClassB.prototype = new ClassA();
ClassB.prototype.name = '';
ClassB.prototype.sayName = function() {
alert(this.name);
};
混合方式:
function ClassA(sColor) {
this.color = sColor;
}
ClassA.prototype.sayColor = function() {
alert(this.color);
};
function ClassB(sColor, sName) {
ClassA.call(this, sColor);
this.name = sName;
}
ClassB.prototype = new ClassA();
ClassB.prototype.sayName = function() {
alert(this.name);
}
朋友写的一种继承方式,感觉比上面几种都要好:
Function.prototype.extend = function (Base) {
var Prototype = function () {};
Prototype.prototype = Base.prototype;
this.prototype = new Prototype();
this.prototype.base = Base;
this.prototype.constructor = this;
}
function Bird(name) {
this.name = name;
}
Bird.prototype.fly = function() {
alert(this.name + " is flying");
}
function Duck(name, owner) {
this.base(name);
this.owner = owner;
}
Duck.extend(Bird);
Duck.prototype.fly = function () {
alert(this.name + " is a duck!");
}
new Duck("duck A").fly();
但这种写法有个问题
就是无法多层继承
虽然又加了点东西可以实现多层继承了
但还是不如上面其他方式的写法好(个人感觉)