JS中prototype属性-JS原型模式

  1 /*
  2  *对象方法
  3  *类方法
  4 * 原型方法
  5 */
  6     function People(name) {
  7         this.name = name;
  8         this.say = function () {  //对象方法
  9             alert("my name is  "+this.name);
 10         }
 11     }
 12
 13     People.run = function () {  //类方法(静态方法,只能由类名调用)
 14         alert("i can run");
 15     }
 16
 17     People.prototype.sayChinese = function () {//原型方法
 18         alert("我的名字是:"+this.name);
 19     }
 20
 21     var p1 = new People("menglinghua");
 22     p1.say();
 23     People.run();
 24     p1.sayChinese();
 25
 26     p1.prototype  //无法获取????????
 27     p1.run();  //无法调用???
 28     p1.sayChinese(); //可以调用
 29
 30     People.say();//无法调用????????
 31     People.sayChinese();//无法调用????????
 32
 33     People.prototype.say();//无法调用????????
 34     People.prototype.run();//无法调用????????
 35
 36
 37 /*
 38 *eg1
 39 */
 40 function baseClass()
 41 {
 42     this.showMessage = function () {
 43         alert("baseClass::showMessage()");
 44     }
 45 }
 46
 47 function extendClass()
 48 {
 49
 50 }
 51
 52 extendClass.prototype = new baseClass();
 53 var extendClassObj = new extendClass();
 54 extendClassObj.showMessage(); //输出"baseClass::showMessage()"
 55
 56
 57 /*
 58 *eg2 两个类含有同名方法
 59 */
 60 function baseClass() {
 61     this.showMessage = function () {
 62         alert("baseClass::showMessage( )");
 63     }
 64 }
 65
 66 function extendClass() {
 67     this.showMessage = function () {
 68         alert("extendClass::showMessage( )");
 69     }
 70 }
 71
 72 extendClass.prototype = new baseClass();
 73 var extendClassObj = new extendClass();
 74 extendClassObj.showMessage();  //输出"extendClass::showMessage( )"
 75
 76 /*
 77 *eg3 让extendClass的实例调用baseClass的同名方法
 78 */
 79 function baseClass() {
 80     this.showMessage = function () {
 81         alert("baseClass::showMessage( )");
 82     }
 83 }
 84
 85 function extendClass() {
 86     this.showMessage = function () {
 87         alert("extendClass::showMessage( )");
 88     }
 89 }
 90
 91 extendClass.prototype = new baseClass();
 92 var extendClassObj = new extendClass();
 93 var baseClassObj = new baseClass();
 94 baseClassObj.showMessage.call(extendClassObj);//让【extendClassObj】调用【baseClassObj】的【showMessage】方法
 95 //输出:"baseClass::showMessage( )"
 96
 97 /*
 98 *eg4
 99 */
100 function baseClass()
101 {
102     this.showMessage = function () {
103         alert("baseClass::showMessage( )");
104     }
105     this.baseShowMessage = function () {
106         alert("baseClass::baseShowMessage");
107     }
108 }
109 baseClass.showMessage = function () {
110     alert("baseClass::showMessge static");
111 }
112
113 function extendClass()
114 {
115     this.showMessage = function () {
116         alert("extendClass::showMessage( )");
117     }
118 }
119
120 extendClass.prototype = new baseClass();
121 var extendClassObj = new extendClass();
122
123 extendClassObj.showMessage();//输出:extendClass::showMessage( )
124 extendClassObj.baseShowMessage();//输出:baseClass::baseShowMessage
125
126 baseClass.showMessage.call(extendClassObj);//输出:baseClass::showMessge static
127
128 var baseClassObj = new baseClass();
129 baseClassObj.showMessage.call(extendClassObj);//输出:baseClass::showMessage( )

http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html

时间: 2024-08-05 08:10:04

JS中prototype属性-JS原型模式的相关文章

js中Prototype属性解释及常用方法

1.prototype的定义 javascript中的每个对象都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用. 每一个构造函数都有一个属性叫做原型.这个属性非常有用:为一个特定类声明通用的变量或者函数. 你不需要显式地声明一个prototype属性,因为在每一个构造函数中都有它的存在.你可以看看下面的例子: function Test(){} alert(Test.prototype); // 输出 "Object" 1.

Js中Prototype、__proto__、Constructor、Object、Function关系介绍 ,JS原型

此文来自:http://www.blogjava.net/heavensay/archive/2013/10/20/405440.html Js中Prototype.__proto__.Constructor.Object.Function关系介绍 一    Prototype.__proto__与Object.Function关系介绍        Function.Object:Js自带的函数对象. prototype,每一个函数对象都有一个显示的prototype属性,它代表了对象的原型(

Js中Prototype、__proto__、Constructor、Object、Function关系介绍

Js中Prototype.__proto__.Constructor.Object.Function关系介绍 一    Prototype.__proto__与Object.Function关系介绍        Function.Object:Js自带的函数对象. prototype,每一个函数对象都有一个显示的prototype属性,它代表了对象的原型(Function.prototype函数对象是个例外,没有prototype属性). __proto__:每个对象都有一个名为__proto

简单理解js的prototype属性

在进入正文之前,我得先说说我认识js的prototype这个东西的曲折过程. 百度js的prototype的文章,先看看,W3School关于prototype的介绍: 你觉得这概念适合定义js的prototype这个东西吗?你是否也认为prototype是一个object对象的属性呢?是的话,请认真认真看我这篇文章,因为这篇文章会毁灭你的人生三观,呵呵,就是有这么严重,因为本人就是被这个定义给害惨的. 不得不说,看了网上的一些介绍prototype的文章,基本上都说prototype是对象的一

js中prototype用法(转)

JavaScript能够实现的面向对象的特征有:·公有属性(public field)·公有方法(public Method)·私有属性(private field)·私有方法(private field)·方法重载(method overload)·构造函数(constructor)·事件(event)·单一继承(single inherit)·子类重写父类的属性或方法(override)·静态属性或方法(static member) 例子一(JavaScript中允许添加行为的类型):可以在

js中prototype,__proto__,constructor之间的关系

首先,我们需要了解三点: 1. 只要创建一个任意新函数,就会根据一个prototype属性,该属性指向函数的原型对象: 2. 每一个原型对象都会自动获得一个constructor属性,该属性只想prototype所在函数的指针: 3. 当调用构造函数创建实例时,该实例内部将包含一个指向构造函数原型对象的指针,在大部分浏览器中用__proto__标识: 从上面这三点我们可以了解到: 正常情况下:prototype属性 -> 原型对象 原型对象的constructor属性 -> 构造函数(即拥有p

JS中style属性

JS中style属性现在我需要对这个标签赋值,其内容为: 1.需要显示的字为"HELLO WORLD": 2.span的 background-color : red ,另外还要:border:1px solid #333333;cursor:hand; 我需要在<script></script>内把他们赋值,请问怎么写呢?难道要: document.getElementById("a").style.background="red

【微信小程序】在js中导入第三方js或自己写的js,使用外部js中的function的两种方法 import和require的区别使用方法

如下 定义了一个外部js文件,其中有一个function import lunaCommon from '../lunaCommon.js'; var ctx = wx.getStorageSync("ctx"); var filter = "/ms-code"; var apis = { //根据sc获取发货单 "findDispatchBill": function (data, success) { var url = ctx + filt

浅谈JS中的构造函数、原型对象(prototype)、实例中的属性/方法之间的关系

原文链接:https://segmentfault.com/a/1190000016951069 构造函数:函数中的一种,通过关键字new可以创建其实例.为了便于区分,通常首字母大写:原型对象:一种特殊的对象,构造函数创建时自动生成:与构造函数形成一一对应,如同人和影子般的关系:实例:通过构造函数实例出来的对象: 在定义构造函数时,在其内部(“{“和”}”)进行定义属性和方法.当我们通过关键字new,对构造函数进行实例化的时候.实例会对构造函数的这些属性进行拷贝出一份副本,然后将其归属为当前实例