在java中,我们可以这么实例化一个对象
MyClass myclass1 = new MyClass();
在javascript中,我们可以这么做
var myclass1 = new MyClass();
但是在javascript中,我们并没有定义MyClass类,而是定义了一个同名的函数。也就是一个构造函数
function myClass(name,height){
this.name = name;
this.height = height;
}
然后,我们就可以这么调用
var myclass1 = new myClass("lds",170);
alert(" name :" + myclass1.name + "\n height:" + myclass1.height );
在构造函数中,设置为this的属性的任何东西都可以作为对象的一个成员来使用。我们可以在构造函数中加入函数的声明。
function myClass(name,height){
this.name = name;
this.height = height;
this.tellHeight = function(){
alert(" name :" + this.name + "\n height:" + this.height );
};
}
var myclass1 = new myClass("lds",170);
myclass1.tellHeight();
每个javascript对象都是相同的基类实例。所以,有可能在运行时给对象分配任意的属性。
写到这里,我们已经看到了类是怎么创建的,但是怎么继承呢?
javascript是没有继承的概念的。但是提供了一些实现方法,使得看起来我们实现了内部继承
prototype是javascript对象的属性,有面向对象语言中没有对等物。函数和属性可以与构造函数的prototype关联起来。然后prototype和new关键字协同工作,当使用new调用函数时,函数prototype的所有属性和方法会附加到结果对象上。
function myClass(name,height){
this.name = name;
this.height = height;
}
myClass.prototype.tellHeight = function(){
alert(" name :" + this.name + "\n height:" + this.height );
}
var myclass1 = new myClass("lds",170);
myClass1.tellHeight();
我们像以前一定声明了构造函数,然后向prototype增加函数。当我们创建一个对象的实例时,函数会附加在上面。关键字this是在运行时确定了对象的实例,一切都运转良好.
在声明构造函数之后,我们才可以引用prototype。对象只能继承在调用构造函数之前已经增加到prototype属性的东西。
[b]用prototype属性扩展内建类[/b]
看下面的例子
Array.prototype.indexOf = function(obj){
var result = -1;
for(var i = 0; i< this.length; i++){
if(this[i] ) = obj){
result = i;
break;
}
}
return result;
}
以上代码为Array对象提供了一个新的函数,我们经常有用到的indexOf。
对象的使用过程之中,随时可以使用prototype函数。但是建议prototype只在程序的开始使用一次,以便壁免不必要的混乱。特别是在团队中使用中javascript对象。
我们已经知道,javascript中是没有直接实现类继承的东西的,但是我们可以通过prototype来实现
prototype的继承
MyClass myclass1 = new MyClass();
在javascript中,我们可以这么做
var myclass1 = new MyClass();
但是在javascript中,我们并没有定义MyClass类,而是定义了一个同名的函数。也就是一个构造函数
function myClass(name,height){
this.name = name;
this.height = height;
}
然后,我们就可以这么调用
var myclass1 = new myClass("lds",170);
alert(" name :" + myclass1.name + "\n height:" + myclass1.height );
在构造函数中,设置为this的属性的任何东西都可以作为对象的一个成员来使用。我们可以在构造函数中加入函数的声明。
function myClass(name,height){
this.name = name;
this.height = height;
this.tellHeight = function(){
alert(" name :" + this.name + "\n height:" + this.height );
};
}
var myclass1 = new myClass("lds",170);
myclass1.tellHeight();
每个javascript对象都是相同的基类实例。所以,有可能在运行时给对象分配任意的属性。
写到这里,我们已经看到了类是怎么创建的,但是怎么继承呢?
javascript是没有继承的概念的。但是提供了一些实现方法,使得看起来我们实现了内部继承
prototype是javascript对象的属性,有面向对象语言中没有对等物。函数和属性可以与构造函数的prototype关联起来。然后prototype和new关键字协同工作,当使用new调用函数时,函数prototype的所有属性和方法会附加到结果对象上。
function myClass(name,height){
this.name = name;
this.height = height;
}
myClass.prototype.tellHeight = function(){
alert(" name :" + this.name + "\n height:" + this.height );
}
var myclass1 = new myClass("lds",170);
myClass1.tellHeight();
我们像以前一定声明了构造函数,然后向prototype增加函数。当我们创建一个对象的实例时,函数会附加在上面。关键字this是在运行时确定了对象的实例,一切都运转良好.
在声明构造函数之后,我们才可以引用prototype。对象只能继承在调用构造函数之前已经增加到prototype属性的东西。
[b]用prototype属性扩展内建类[/b]
看下面的例子
Array.prototype.indexOf = function(obj){
var result = -1;
for(var i = 0; i< this.length; i++){
if(this[i] ) = obj){
result = i;
break;
}
}
return result;
}
以上代码为Array对象提供了一个新的函数,我们经常有用到的indexOf。
对象的使用过程之中,随时可以使用prototype函数。但是建议prototype只在程序的开始使用一次,以便壁免不必要的混乱。特别是在团队中使用中javascript对象。
我们已经知道,javascript中是没有直接实现类继承的东西的,但是我们可以通过prototype来实现
prototype的继承