JavaScript 构建类和创建对象的方式
1>工厂函数来创建对象(Factory)
function createCar(){
var obj = new Object;
obj.name = "BWM";
obj.show = function(){
alert(this.name);
}
return obj;
}
var car1 = createCar();
var car2 = createCar();
car1.show();
car2.show();
2>构造函数创建对象(Constructor)
function Car(name){
this.name = name;
this.show = function(){
alert(this.name);
}
}
Car.prototype.say = function(){
alert("hello");
}
var car = new Car("BMW");
car.show();
car.say();
3>原型(prototype)创建对象
function Car(){}
Car.prototype.name = "BMW";
Car.prototype.show = function(){
alert(this.name);
}
Car.prototype.say = function(){
alert("Hiloo");
}
var car = new Car();
car.show(); car.say();
4>构造 && 原型:推荐使用,但是理解起来有点模糊
/*
构造函数是来初始化非函数属性的
*/
function Car(name){
this.name = name;
}
Car.prototype.show = function(){
alert(this.name);
}
Car.prototype.say = function(){
alert("hiloo");
}
var car = new Car("BMW");
car.show(); car.say();
5>Dynamic Prototype:类似于java中类的创建,是学习过Java语言程序员常用的方式
function Car(name){
this.name = name;
if(typeof Car._initialized == "undefined"){
Car.prototype.show = function(){
alert(this.name);
}
Car._initialized = true;//改变目的是,执行一次
}
}
var car = new Car("BMW");
car.show();
相关文档:
1.document.formName.item("itemName") 问题
说明:IE下,可以使用document.formName.item("itemName")或document.formName.elements["elementName"];Firefox下,只能使用document.formName.elements["elementName"].
解决方法:统一使用document.formName.elements["elementName"].
2.集合类对象问题
说明:IE下,可以使用() ......
继承机制,说到继承,就要想到从基类开始,但是JavaScript中的类有两大类:一,内嵌的;二,用户自定义;一般前者不会被用来做基类,原因是为了保障js的安全。但是我们可以通过prototype来对基类进行扩充,增加我们想要的属性和方法。以下是自己对继承的几种方式的理解。
1> 对象 ......
1>zInherit:
它是一个组件,用来继承基类的所有属性和方法。跟以前说到的原型链模式非常类似,只不过比原型更安全,也无须考虑参数问题。下面看看zInherit的用法:
该组件中只有两个方法:inheritfrom() instanceof()
func ......
每次用的时候都要找一遍资料,琢磨一遍语法,不胜其烦,终于下定决心把他们都记下来,一次性搞定,永绝后患!:)
一、什么是正则表达式?
简单地说,就是基于字符串的模式匹配工具。实际应用中包括字符串的查找、提取、替换等等。
二、基本语法
Javascript中的正则表达式的形式一 ......
(一)对象冒充
function A(name){
this.name = name;
this.sayHello = function(){alert(this.name+” say Hello!”);};
}
function B(name,id){
this.temp = A;
this.temp(name); &nbs ......