prototype
总结:1.prototype 是为实例制定属性和方法,通过baseClass.Prototype.property=3,可以给实例额外添加属性,但是如果原有这个属性,这种方法失效
2.baseClass.Prototype=new fatherClass();可以进行克隆,并且可以执行1中的函数,达到重写父类方法的效果
3.切记prototype是为类创建实例的方法
源码 | 说明 | |
---|---|---|
1 |
Object.prototype.Property = 1;
Object.prototype.Method = function ()
{
alert(1);
}
var obj = new Object();
alert(obj.Property);
obj.Method();
| 可以在类型上使用proptotype来为类型添加行为。这些行为只能在类型的实例上体现。 JS中允许的类型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String |
2 |
var obj = new Object();
obj.prototype.Property = 1; //Error
//Error
obj.prototype.Method = function()
{
alert(1);
}
| 在实例上不能使用prototype,否则发生编译错误 |
3 |
Object.Property = 1;
Object.Method = function()
{
alert(1);
}
alert(Object.Property);
Object.Method();
| 可以为类型定义“静态”的属性和方法,直接在类型上调用即可 在function baseClass(){ baseClass.obj=1; baseClass.MethodName=functon(){} //定义静态属性和方法 } |
4 |
Object.Property = 1;
Object.Method = function()
{
alert(1);
}
var obj = new Object();
alert(obj.Property); //Error
obj.Method(); //Error
| 实例不能调用类型的静态属性或方法,否则发生对象未定义的错误。 |
5 |
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
var obj = new Aclass();
alert(obj.Property);
obj.Method();
| 这个例子演示了通常的在JavaScript中定义一个类型的方法 |
6 |
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
Aclass.prototype.Property2 = 2;
Aclass.prototype.Method2 = function
{
alert(2);
}
var obj = new Aclass();
alert(obj.Property2);
obj.Method2();
| 可以在外部使用prototype为自定义的类型添加属性和方法。 |
7 |
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
Aclass.prototype.Property = 2;
Aclass.prototype.Method = function
{
alert(2);
}
var obj = new Aclass();
alert(obj.Property);
obj.Method();
|
在外部不能通过prototype改变自定义类型的属性或方法。
该例子可以看到:调用的属性和方法仍是最初定义的结果。
它会先去本身找,如果找不到,就会去克隆的类中找。 |
8 |
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
var obj = new Aclass();
obj.Property = 2;
obj.Method = function()
{
alert(2);
}
alert(obj.Property);
obj.Method();
|
可以在对象上改变属性。(这个是肯定的)
也可以在对象上改变方法。(和普遍的面向对象的概念不同)
|
9 |
function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
var obj = new Aclass();
obj.Property2 = 2;
obj.Method2 = function()
{
alert(2);
}
alert(obj.Property2);
obj.Method2();
| 可以在对象上增加属性或方法 |
10 |
function AClass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
function AClass2()
{
this.Property2 = 2;
this.Method2 = function()
{
alert(2);
}
}
AClass2.prototype = new AClass();
var obj = new AClass2();
alert(obj.Property);
obj.Method();
alert(obj.Property2);
obj.Method2();
| 这个例子说明了一个类型如何从另一个类型继承。 |
11 |
function AClass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
function AClass2()
{
this.Property2 = 2;
this.Method2 = function()
{
alert(2);
}
}
AClass2.prototype = new AClass();
AClass2.prototype.Property = 3;
AClass2.prototype.Method = function()
{
alert(4);
}
var obj = new AClass2();
alert(obj.Property);
obj.Method();
| 这个例子说明了子类如何重写父类的属性或方法。 |
2.baseClass.Prototype=new fatherClass();可以进行克隆,并且可以执行1中的函数,达到重写父类方法的效果
3.切记prototype是为类创建实例的方法
call 讲解
解释:将将instance当做baseinstance来调用,调用它的对象方法showMsg,如果想调用类方法,那么前面的baseinstance就不能是实例
extendClass.prototype = new baseClass();
var instance = new extendClass();
var baseinstance = new baseClass();
baseinstance.showMsg.call(instance);//显示baseClass::showMsg
解释:将将instance当做baseinstance来调用,调用它的对象方法showMsg,如果想调用类方法,那么前面的baseinstance就不能是实例
全局变量写法
<script type="text/javascript">
//全局变量;
var sltDailt = {
"name" : "Jack",
age : 21,
Dailt : function(ipt) {
alert(this.age);
}
}
function car(thecolor, thenumber, thewheels) {
this.methodName = "methodName";
this.color = thecolor;
this.number = thenumber;
this.wheels = thewheels;
this.Method1=function(){alert("methodName--car");}
}
var mycar = new car("RED", "13245", 4);
//sltDailt.Dailt("JsTest.");
//alert(mycar.color);
//alert(mycar.methodName);
//mycar.Method1();
</script>