javascript面向对象技术基础(五)
类变量/类方法/实例变量/实例方法
先补充一下以前写过的方法:
在javascript中,所有的方法都有一个call方法和apply方法.这两个方法可以模拟对象调用方法.它的第一个参数是对象,后面的
参数表示对象调用这个方法时的参数(ECMAScript specifies two methods that are defined for all functions, call()
and apply(). These methods allow you to invoke a function as if it were a method of some other object. The first
argument to both call() and apply() is the object on which the function is to be invoked; this argument becomes
the value of the this keyword within the body of the function. Any remaining arguments to call() are the values
that are passed to the function that is invoked).比如我们定义了一个方法f(),然后调用下面的语句:
f.call(o, 1, 2);
作用就相当于
o.m = f;
o.m(1,2);
delete o.m;
举个例子:
?---------------------------------
实例变量和实例方法都是通过实例对象加"."操作符然后跟上属性名或方法名来访问的,但是我们也可以为类来设置方法或变量,
这样就可以直接用类名加"."操作符然后跟上属性名或方法名来访问.定义类属性和类方法很简单:?
prototype属性的应用:
19 楼 yza0088 2008-12-24 楼主第一次让我感觉到js的可怕 呵呵 20 楼 hite 2008-12-26 daoyongyu 写道主,辛苦了.的很好,易懂!!
下面这个例子是根据原书改过来的.
假设我们定义了一个Circle类,有一个radius属性和area方法,实现如下:
这个系列基本看完了。
ps:贴主感觉繁体很帅吗?还是因为是港澳人士?小小的不爽而已。没事没事。
21 楼 liliefeng 2008-12-29 楼主的代码有很多错误,很明显的一个就是this的使用,其实this在js中是关联执行时的作用域,而非定义时的作用域,呵呵,好好看看,别误导新手 22 楼 sdcyst 2008-12-30 liliefeng 写道
楼主的代码有很多错误,很明显的一个就是this的使用,其实this在js中是关联执行时的作用域,而非定义时的作用域,呵呵,好好看看,别误导新手
this的作用域是在js关联执行时确定的,但是在文章中我并没有提到这方面的内容。而且,如果代码有错误的话,那还请你详细的指出来,我肯定非常感谢 23 楼 hanjs 2008-12-30 f.call(o, 1, 2);
作用就相当于
o.m = f;
o.m(1,2);
delete o.m; //这个是什么时候执行的?
我怎么通过代码看是这样的情况呢?
o=f(1,2);
24 楼 sdcyst 2008-12-30 hanjs 写道f.call(o, 1, 2);
作用就相当于
o.m = f;
o.m(1,2);
delete o.m; //这个是什么时候执行的?
我怎么通过代码看是这样的情况呢?
o=f(1,2);
f.call(o, 1, 2);
作用就相当于
o.m = f;
o.m(1,2);
delete o.m;
后面的情况是为了描述的更清楚而假设的
你自己假设的情况是不正确的,最好自己试一下。
25 楼 terrysunhh 2009-02-12 请教一下,在最后一段代码中
1. PositionCircle.prototype = new Circle();
new Circle()是创建一个新的Circle实例还是设置PositionCircle.prototype属性指向Circle函数
2. alert(PositionCircle.constructor);
alert(PositionCircle.prototype.constructor)
为什么得到不一样的结果呢?PositionCircle.constructor与PositionCircle.prototype.constructor有什么区别?
3. PositionCircle.prototype.constructor = PositionCircle;
alert(PositionCircle.constructor);
alert(PositionCircle.prototype.constructor)
与
PositionCircle.prototype = new PositionCircle();// 参考PositionCircle.prototype = new Circle();
alert(PositionCircle.constructor);
alert(PositionCircle.prototype.constructor)
输出不一样?请教一下 26 楼 Blithe 2009-02-20 terrysunhh 写道
3. PositionCircle.prototype.constructor = PositionCircle;
alert(PositionCircle.constructor);
alert(PositionCircle.prototype.constructor)
与
PositionCircle.prototype = new PositionCircle();// 参考PositionCircle.prototype = new Circle();
alert(PositionCircle.constructor);
alert(PositionCircle.prototype.constructor)
请问这有什么qubie 27 楼 google_fans 2009-04-27 还不错,值得一看啊 28 楼 123003473 2011-10-24 function Parent(name){
this.name = name;
}
Parent.prototype.say = function(){
alert('我的名字:'+this.name);
}
Parent.prototype.sayGrade= function(){
alert('我已经不上学了');
}
function Student(grade,name){
this.grade = grade;
Parent.call(this,name);
}
Student.prototype = new Parent();
Student.prototype.constructor = Student;
Student.prototype.super=Parent.prototype;
Student.prototype.sayGrade = function(){
this.super.sayGrade();
alert('我上'+this.grade);
}
var stu1 =new Student('1','胡');
stu1.say();
stu1.sayGrade();
stu1.hasOwnProperty('name');