欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » Html » 正文

热门阅读

JavaScript开发技术(三)-面向对象编程

创建时间:2012年05月09日 14:19  阅读次数:(3902)
分享到:

四、Javascript面向对象编程


1.构造函数
下面的方法可以构造一个Person类。
function Person(name, age) {
    this.Name = name;
    this.Age = age;
    this.GetName = function() {
        return this.Name;
    };
}

下面的方法可以构造一个Person2类。GetName()方法会出现在智能提示中。
function Person2(name, age) {
    this.Name = name;
    this.Age = age;
}
Person2.prototype.GetName = function() {
    return this.Name;
};

2.继承
(1)方式1
//定义Person类
function Person(name, age) {
    this.Name = name;
    this.Age = age;
    this.GetName = function() {
        return this.Name;
    };
}
//定义Employee类
function Employee(name, age, employeeID) {
    this.Name = name;
    this.Age = age;
    this.EmployeeID = employeeID;
}
//使Employee类继承自Person类
Employee.prototype = new Person();
//为Employee类创建新方法
Employee.prototype.GetEmployeeID = function() {
    return this.EmployeeID;
};
//增强Employee类原方法
Employee.prototype.GetName = function() {
    return "Name="+this.Name+";EmployeeID="+this.EmployeeID;
};

(2)方式2 对象冒充
function A(name){    
this.name = name;    
this.sayHello = function(){
alert(this.name+” say Hello!”);
};
}
function B(name,id){    
this.temp = A;    
this.temp(name);        //相当于new A();    
delete this.temp;        //防止在以后通过temp引用覆盖超类A的属性和方法     this.id = id;        
this.checkId = function(ID){
alert(this.id==ID)
};
}

所有新属性和新方法都必须在删除了新方法的代码行后定义。否则,可能会覆盖超类的相关属性和方法。
对象冒充可以支持多重继承。也就是说,一个类可以继承多个超类。
function ClassZ() {
    this.newMethod = ClassX;
    this.newMethod();
    delete this.newMethod;

    this.newMethod = ClassY;
    this.newMethod();
    delete this.newMethod;
}

3.this代表的对象
this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window; 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用。
例:
//创建GetLocation全局函数
function GetLocation() {
    return this.location;
}
alert(GetLocation()); //返回window的location属性
//创建Office类
function Office() {
    location: "";
    this.GetLocation = function() {
        return this.location;
    };
}
//定义office变量
var office = new Office();
office.location = "Office Location";
alert(office.GetLocation()); //返回"Office Location"

可以通过apply方法或call方法改变this的指向
alert(GetLocation.apply(window)); //返回window的location属性
alert(GetLocation.apply(office)); //返回"Office Location"
alert(GetLocation.call(window)); //返回window的location属性
alert(GetLocation.call(office)); //返回"Office Location"

4.对象的销毁
把对象的所有引用都设置为 null,可以强制性地废除对象。例如:
var oObject = new Object;
// do something with the object here
oObject = null;

5.对象方法和属性都是公用的
开发约定(非强制性),私有属性应加下划线(前后都加或只加一个)
例:_userName_  _userName  userName_

6.工厂方法
//工厂方法
function createPerson(name, age) {
    var person = new Object;
    person.Name = name;
    person.Age = age;
    person.GetName = function() {
        return this.Name;
    };
    return person;
}

var p1 = createPerson("kang", 36);
var p2 = createPerson("zhang", 24);

7.超类Object
(1)属性
constructor 
对创建对象的函数的引用(指针)。对于 Object 对象,该指针指向原始的 Object() 函数。 
Prototype 
对该对象的对象原型的引用。对于所有的对象,它默认返回 Object 对象的一个实例。

(2)方法
hasOwnProperty(property) --判断对象是否有某个特定的属性。必须用字符串指定该属性。(例如,o.hasOwnProperty("name")) 
IsPrototypeOf(object) --判断该对象是否为另一个对象的原型。 
PropertyIsEnumerable --判断给定的属性是否可以用 for...in 语句进行枚举。 
ToString() --返回对象的原始字符串表示。对于 Object 对象,ECMA-262 没有定义这个值,所以不同的 ECMAScript 实现具有不同的值。 
ValueOf()--返回最适合该对象的原始值。对于许多对象,该方法返回的值都与 ToString() 的返回值相同。

8.对象属性的另类定义方法
var myObj = new Object();
myObj.Name = "sa";//正常定义属性
myObj["XXXXXX"]=true;// 必须写在方括号里,有空格时要用引号n
来源:.net学习网
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 97+12=? 的结果(结果是:109)
结  果: