一个关于js中prototype的小实例
2009-09-10 14:19
477 查看
什么是prototype呢?看书,GOOGLE之,还问了一只很特别的鸟,学到了一点点,如下面的小实例:
<html>
<body>
<script lang="text/javascript">
function father(){
this.plus_other = function(){
alert('plus_other: ' + this.cityId);
}
}
function City(){
this.plus_other = function(){
alert('plus_other!');
}
}
function City2(){}
City.prototype = father;
City2.prototype = father;
City.prototype.plus = function(){
alert('plus!');
}
var city1 = new City();
city1.plus_other();
var city2 = new City2();
//city2.plus_other(); 没有这个方法,所以这行会报错
city.plus();
</script>
</body>
</html>
其实prototype(原型)就是类似于基类……,而使用 “某对象A.prototype.XXX” 的方法就是把新的方法或者属性定义到 “A对象”的原型(类似于父类、基类)中去。所以city2对象也有plus方法,但是city2对象没有plus_other方法。
prototype还有什么好玩的特性呢?……学习研究中……
<html>
<body>
<script lang="text/javascript">
function father(){
this.plus_other = function(){
alert('plus_other: ' + this.cityId);
}
}
function City(){
this.plus_other = function(){
alert('plus_other!');
}
}
function City2(){}
City.prototype = father;
City2.prototype = father;
City.prototype.plus = function(){
alert('plus!');
}
var city1 = new City();
city1.plus_other();
var city2 = new City2();
//city2.plus_other(); 没有这个方法,所以这行会报错
city.plus();
</script>
</body>
</html>
其实prototype(原型)就是类似于基类……,而使用 “某对象A.prototype.XXX” 的方法就是把新的方法或者属性定义到 “A对象”的原型(类似于父类、基类)中去。所以city2对象也有plus方法,但是city2对象没有plus_other方法。
prototype还有什么好玩的特性呢?……学习研究中……
相关文章推荐
- 一个能说明问题的js的原型 prototype的实例--简单易懂
- js中对prototype对象继承的一个实例
- 关于JS call apply 对象、对象实例、prototype、Constructor、__proto__
- 【推荐】关于JS中的constructor与prototype【转】
- Prototype中的shallow clone 与deep clone 与new 一个实例的区别
- 【推荐】关于JS中的constructor与prototype【转】
- 关于委托、事件、处理事件的方法以及.NET 续一个实例
- JS中关于call()方法的一个问题(转自百度知道,解答非常详细清楚)
- js查看一个函数的执行时间实例代码
- 每日一个js实例10--回车键
- js利用for in循环获取 一个对象的所有属性以及值的实例
- Win2003下关于c#读写Excel时,出现存取被拒&Excel实例化出错的一个解决方法
- [转]QTP中一个关于Action的实例
- prototype.js1.5.0开发者手册 译文+添加注释(四)关于ajax类的
- 关于一个JS加法函数的应用
- js中关于prototype属性
- 【推荐】关于JS中的constructor与prototype【转】
- prototype.js的系列文章——关于prototype.js
- 【推荐】关于JS中的constructor与prototype【转】
- js关于prototype