js中的面向对象的prototype,call的用法
来源:互联网 发布:淘宝沟通服务 编辑:程序博客网 时间:2024/05/21 16:40
javascript可以实现面向对象的思想的用法。其中prototype是常见的关键字,它的含义可以实际上是“引用”,而非“赋值”。也就是给一个类添加一个属性或者方法,是给它添加了个引用,而非赋值一份给它。
例子如下:
<script type="text/javascript">function MyObject(name,size){//方法可以写为一个对象的形式
this.name=name;
this.size=size;
}
MyObject.prototype.height="2.5m"; //引用对象,并扩展属性
MyObject.prototype.tellHeight=function(){//引用对象,并扩展方法
return "height of "+this.name+"is "+this.height;
}
var myobj1= new MyObject("box",3);
if(myobj1.tellHeight){
alert(myobj1.tellHeight());//显示 height of box is 2.5m
}
</script>
function MyApplication() {
this.counter = 0;
}
MyApplication.prototype.onMapClick = function() {
this.counter++;
alert("这是您第 " + this.counter + " 次点击地图");
}
this.counter = 0;
}
MyApplication.prototype.onMapClick = function() {
this.counter++;
alert("这是您第 " + this.counter + " 次点击地图");
}
function ClassA() {
alert("a");
this.a=function(){alert();};
}
function ClassB() {
alert("b");
this.b=function(){alert();};
}
ClassB.prototype.a=new ClassA(); //会导致弹出 a 对话框
ClassB.prototype.xx = "xx";
function initialize() {
var objB1=new ClassB(); //弹出 b 对话框
var objB2=new ClassB(); //弹出 b 对话框
alert(objB1.a==objB2.a); //true
alert(objB1.b==objB2.b); //false
alert("objB1.xx: " + objB1.xx + ", objB2.xx: " + objB2.xx); //objB1.xx: xx, objB2.xx: xx
ClassB.prototype.xx = "yy";
alert("objB1.xx: " + objB1.xx + ", objB2.xx: " + objB2.xx); //objB1.xx: yy, objB2.xx: yy
objB2.xx = "zz";
alert("objB1.xx: " + objB1.xx + ", objB2.xx: " + objB2.xx); //objB1.xx: yy, objB2.xx: zz
}
initialize();
</script>prototype、call的用法<script type="text/javascript">
function baseClass(){
this.showMsg = function() {
alert("baseClass::showMsg");
}
this.baseShowMsg = function() {
alert("baseClass::baseShowMsg");
}
}
baseClass.showMsg = function(){
alert("baseClass::showMsg static");
}
function extendClass(){
this.showMsg =function () {
alert("extendClass::showMsg");
}
}
extendClass.showMsg = function(){
alert("extendClass::showMsg static")
}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg(); //显示extendClass::showMsg
instance.baseShowMsg(); //显示baseClass::baseShowMsg
instance.showMsg(); //显示extendClass::showMsg
baseClass.showMsg.call(instance);//显示baseClass::showMsg static
var baseinstance = new baseClass();
baseinstance.showMsg.call(instance);//显示baseClass::showMsg
</script>
参考:http://blog.csdn.net/xiaoyuemian/article/details/3844305
http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html
0 0
- js中的面向对象的prototype,call的用法
- JS面向对象、prototype、call()、apply()
- JS面向对象、prototype、call()、apply()
- JS面向对象、prototype、call()、apply()
- JS面向对象、prototype、call()、apply()
- js中的prototype的用法
- JS中的prototype(面向对象)
- JS 面向对象之神奇的prototype
- js面向对象的继承实例-call
- JS中Object.prototype.toStrig.call()的用法
- js中继承的几种用法apply,call,prototype
- js中继承的几种用法apply,call,prototype
- Object.prototype.hasOwnProperty.call()计算js对象的长度
- JS面向对象、prototype、call()、apply()和实例
- 深入理解js面向对象中的prototype
- 深入理解js面向对象中的prototype
- js中的prototype对象的理解
- js中的apply与call的用法
- iOS学习之iOS程序名称及内容国际化(本地化)
- BootLoader —— S3C2440
- 黑马程序员——java网络编程中的传输协议:UDP和TCP
- hdu3826(素数的应用——判断某个数的因子是否含有整数的平方数)
- 看书有点浮漂
- js中的面向对象的prototype,call的用法
- 1025. PAT Ranking (25)
- ssh加密方式和telnet
- 【好文分享】用增量更新算法为 web 应用节省流量
- java学习笔记<十二>枚举类
- Wince之路:5.指数坐标系
- 使用 GitHub, Jekyll 打造自己的免费独立博客
- 如何部署war文件到tomcat下
- hdu-4780-Candy Factory--费用流