1. /*
  2. //工厂模式
  3. function createObject(name,age){
  4. var obj = new Object();//新建一个对象
  5. obj.name=name;//新建对象的属性
  6. obj.age=age;
  7. obj.run=function(){//新建对象的方法
  8. return this.name+","+this.age+"...running.";
  9. }
  10. return obj;//返回新建的对象引用
  11. }
  12.  
  13. var box1 = createObject("Carl",20);
  14. var box2 = createObject("Zhang",25);
  15. alert(box1.run());
  16. alert(box2.run());
  17.  
  18. //构造函数
  19. function Box(name,age){
  20. this.name=name;//新建对象的属性
  21. this.age=age;
  22. this.run=function(){//新建对象的方法
  23. return this.name+","+this.age+"...running.";
  24. }
  25.  
  26. }
  27.  
  28. var box1 = new Box("Carl",20);//对象实例化
  29. var box2 = new Box("Zhang",25);
  30. alert(box1.run());
  31. alert(box2.run());
  32. alert(box1 instanceof Object);
  33.  
  34. //构造函数
  35. function box(name,age){
  36. this.name=name;//新建对象的属性
  37. this.age=age;
  38. this.run=function(){//新建对象的方法
  39. return this.name+","+this.age+"...running.";
  40. }
  41.  
  42. }
  43.  
  44. var box1 = new box("Carl",20);//对象实例化
  45. var box2 = new box("Zhang",25);
  46. alert(box1.run());
  47. alert(box2.run());
  48. alert(box1 instanceof Object);
  49.  
  50. //构造函数
  51. function Box(name,age){
  52. this.name=name;//新建对象的属性
  53. this.age=age;
  54. this.run=function(){//新建对象的方法
  55. return this.name+","+this.age+"...running.";
  56. }
  57.  
  58. }
  59.  
  60. var o = new Object();
  61. Box.call(o,"Carl",20);//call 对象冒充
  62. alert(o.run())
  63.  
  64. //构造函数
  65. function Box(name,age){
  66. this.name=name;//新建对象的属性
  67. this.age=age;
  68. this.run=new Function("return this.name+this.age+'..running...'");
  69.  
  70. }
  71.  
  72. var o = new Object();
  73. Box.call(o,"Carl",20);//call 对象冒充
  74. alert(o.run());
  75.  
  76. //构造函数
  77. function Box(name){
  78. this.name=name;//实例属性
  79. }
  80. Box.prototype.name="Zhang";//原型属性
  81. Box.prototype.age=25;//原型属性
  82. Box.prototype.run=function(){//原型方法
  83. return this.name+","+this.age+"...running.";
  84. }
  85.  
  86. //构造函数体内的叫实例属性,每次新建的对象,其实例属性所在地址都是不一样的
  87. //prototype是一个引用,指向一个对象,通过prototype创建的属性,叫做原型属性,每次新建的对象的原型属性地址是一样的。
  88.  
  89. var box1 = new Box("Carl");//对象实例化
  90. alert(box1.run());
  91. alert(box1.constructor);//构造函数,返回构造函数本身
  92. alert(box1.hasOwnProperty("name"));//判断实例属性中是否含有name属性
  93. delete box1.name;//删除实例属性
  94. alert("name" in box1); //true 判断是否存在于实例属性或者原型属性
  95.  
  96. //构造函数
  97. function Box(name){
  98. this.name=name;//实例属性
  99. }
  100.  
  101. Box.prototype={
  102. name:"Zhang",//原型属性
  103. age:25,//原型属性
  104. run:function(){//原型方法
  105. return this.name+","+this.age+"...running.";
  106. }
  107. }
  108. var box = new Box();
  109. alert(box.constructor);//Ojbect
  110.  
  111. //构造函数
  112. function Box(name){
  113. this.name=name;//实例属性
  114. }
  115.  
  116. Box.prototype={
  117. constructor:Box,
  118. name:"Zhang",//原型属性
  119. age:25,//原型属性
  120. run:function(){//原型方法
  121. return this.name+","+this.age+"...running.";
  122. }
  123. }
  124. var box = new Box();
  125. alert(box.constructor);//Box
  126.  
  127. //构造函数
  128. function Box(name){
  129. this.name=name;//实例属性
  130. }
  131.  
  132. Box.prototype={
  133. constructor:Box,
  134. name:"Zhang",//原型属性
  135. age:25,//原型属性
  136. run:function(){//原型方法
  137. return this.name+","+this.age+"...running.";
  138. }
  139. }
  140. //重写原型
  141. Box.prototyp={
  142. name:"Zhang"
  143. }
  144.  
  145. var box = new Box();
  146. alert(box.run());//error
  147.  
  148. var box=[2,1,29,3,0,13];
  149. alert(box.sort());
  150. alert(Array.prototype.sort);//查看sort是否为Array的原型函数
  151.  
  152. alert(String.prototype.addstring);
  153. String.prototype.addstring=function(){
  154. return this+"...我是扩展的方法";
  155. }
  156. alert("Test addstring".addstring());
  157.  
  158. //动态原型模式
  159. //构造函数
  160. function Box(name,age){
  161. this.name=name;//实例属性
  162. this.age=age;
  163.  
  164. //将原型封装到构造函数中
  165. Box.prototype.run=function(){
  166. return this.name+this.age+"running...";
  167. }
  168. }
  169.  
  170. var box1= new Box("Carl",10);
  171. var box2= new Box("Zhang",20);
  172. alert(box1.run());
  173. alert(box2.run());
  174.  
  175. //构造函数
  176. function Box(name,age){
  177. this.name=name;//实例属性
  178. this.age=age;
  179.  
  180. //将原型封装到构造函数中
  181. if(typeof this.run!='function'){//加上这段可以使得下面的原型只初始化一次
  182. alert("初始化开始");
  183. Box.prototype.run=function(){
  184. return this.name+this.age+"running...";
  185. }
  186. alert("初始化结束");
  187.  
  188. }
  189. }
  190.  
  191. var box1= new Box("Carl",10);
  192. var box2= new Box("Zhang",20);
  193.  
  194. //继承,通过原型链实现
  195. function Box(){ //继承的函数叫做超类(父类,基类)
  196. this.name="Carl";
  197. }
  198. function Desk(){ //继承的函数叫做子类型(子类,派生类)
  199. this.age=100;
  200. }
  201. //通过原型链继承,超类型实例化后的对象实例,赋值给子类型的原型属性
  202. Desk.prototype=new Box();//new Box()会将Box构造里的信息和原型里的信息全都交给Desk
  203.  
  204. var desk = new Desk();
  205. alert(desk.name);
  206.  
  207. */
  208.  
  209. /*
  210. //1.原型链继承 2.借用构造函数继承(对象冒充继承) 3.组合继承(结合前两种)
  211.  
  212. //4.原型式继承
  213.  
  214. //临时中转函数
  215. function obj(o){
  216. function F(){};
  217. F.prototype=o;
  218. return new F();
  219. }
  220.  
  221. box={
  222. name:"Carl",
  223. age:20,
  224. family:["哥哥","姐姐","弟弟"]
  225. };
  226.  
  227. var box1 = obj(box);
  228. alert(box1.family);
  229. box1.family.push("妹妹");
  230. alert(box1.family);
  231.  
  232. var box2 = obj(box);
  233. alert(box2.family);
  234.  
  235. */
  236.  
  237. //临时中转函数
  238. function obj(o){
  239. function F(){};
  240. F.prototype=o;
  241. return new F();
  242. }
  243. //寄生函数
  244. function create(box,desk){
  245. var f=obj(box.prototype);
  246. f.constructor=desk;//调整原型构造指针
  247. desk.prototype=f;
  248. }
  249.  
  250. function Box(name,age){
  251. this.name=name;
  252. this.age=age;
  253. }
  254.  
  255. Box.prototype.run=function(){
  256. return this.name+this.age+" running...";
  257.  
  258. }
  259.  
  260. function Desk(name,age){
  261. Box.call(this,name,age); //对象冒充
  262. }
  263. //通过寄生组合继承来实现继承
  264. create(Box,Desk);//这句话用来代替Desk.prototype=new Box();
  265.  
  266. var desk =new Desk("Carl",20);
  267. alert(desk.run());
  268. alert(desk.constructor);
  269. alert(desk.constructor.prototype);
  270. alert(desk.__proto__===desk.constructor.prototype);

备注:本文部分代码来自李炎辉老师的javascript教程

javascript面向对象和原型的更多相关文章

  1. 第一百零九节,JavaScript面向对象与原型

    JavaScript面向对象与原型 学习要点: 1.学习条件 2.创建对象 3.原型 4.继承 ECMAScript有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标 ...

  2. 有关javaScript面向对象和原型笔记

    javaScript是一种比較特殊的语言,ECMAScript中没有类的概念.跟其它面向对象的语言有一定的差别.它的对象也与基于类的语言中的对象有所不同,严格来说,javascript对象是一组没有特 ...

  3. 了解JavaScript 面向对象基础 & 原型与对象

    面向对象语言中的对象 老是能听到什么基于对象, 面向对象. 什么是对象, 如果有面向对象基础的人可以无视了, 下面举个简单的例子给大家讲讲面向对象中, 对象的定义, 这个是比较通用的, 不过对于JS来 ...

  4. JavaScript面向对象和原型函数

    对象,是javascript中非常重要的一个梗,是否能透彻的理解它直接关系到你对整个javascript体系的基础理解,说白了,javascript就是一群对象在搅..(哔!). 常用的几种对象创建模 ...

  5. JavaScript面向对象与原型

    工厂模式:无法识别对象 function createObject(name, age) { //集中实例化的函数 var obj = new Object(); obj.name = name; o ...

  6. JavaScript 面向对象与原型

    ECMAScript有两种开发模式:1.函数式(过程化);2.面向对象(OOP); 一 创建对象1.普通的创建对象 ? 1 2 3 4 5 6 7 8 9 // 创建一个对象,然后给这个对象新的属性和 ...

  7. 快速理解JavaScript面向对象编程—原型

    总的来说js语言就是门面向对象编程的语言,对象这个概念几乎贯穿了整个js的学习. 对象 创建对象两种方法:(若要生成对象实例必须调用构造函数) 1.var obj = {name:"jer& ...

  8. JavaScript 面向对象之原型对象

    原型的概述 我们创建的每个函数都有一个 prototype(原型)属性,这个属性是一个对象,它的用途是包含可以由特定类型的所有实例共享的属性和方法. 逻辑上可以这么理解:prototype 通过调用构 ...

  9. JavaScript——面向对象与原型

    在最外面使用this,此时this是window作用域下的,因此他指向全局变量 对象冒充: 实例属性不会共享!

随机推荐

  1. COM学习(三)——数据类型

    上回书介绍了GUID.CLSID.IID和接口的概念.本回的重点是介绍 COM 中的数据类型.咋还不介绍组件程序的设计步骤呀?咳......别着急,别着急!孔子曰:"饭要一口一口地吃&quo ...

  2. Windows phone 8 学习笔记(2) 数据文件操作(转)

    Windows phone 8 应用用于数据文件存储访问的位置仅仅限于安装文件夹.本地文件夹(独立存储空间).媒体库和SD卡四个地方.本节主要讲解它们的用法以及相关限制性.另外包括本地数据库的使用方式 ...

  3. 【.NET】对文件的对称加密

    using System;using System.IO;using System.Security.Cryptography;namespace ConsoleApp_SymmetricalEncr ...

  4. 1.3查看Linux内核版本

    1.目前Linux内核主要维护的三个版本:Linux2.4.Linux2.6和Linux3.x,Android使用的是Linux2.6:Linux3.x是最新推出的Linux内核版本: 2.查看Lin ...

  5. javaSE第十三天

    第十三天    76 1. StringBuffer(掌握)    76 (1)说明:    77 (2)StringBuffer的构造方法    77 (3)StringBuffer的常见功能    ...

  6. (一)、NodeJS (转载)

    NodeJS基础                                                 ------ 转载自阿里巴巴 什么是NodeJS JS是脚本语言,脚本语言都需要一个解 ...

  7. win2003以isapi的方式配置php+mysql环境(安装了shopEX)

    一.准备相关组件 mysql-installer-community-5.5.29.0.zip php-5.2.17-Win32-VC6-x86 ZendOptimizer-3.3.3-Windows ...

  8. 刚开始学IOS遇到的类和方法

    框架:Core FoundationCFGetRetainCount. 类:NSRunLoop.NSAutoreleasePool.NSStringFormClass.UIApplicationMai ...

  9. sqoop的job工具

    sqoop job: Work with saved jobs 就是将sqoop的某条语句保存为一个job 1.把person表导入到HDFS上,可以使用下面的语句 sqoop import --co ...

  10. mysql批量修改表引擎

    生成修改的语句 SELECT CONCAT('ALTER TABLE ',table_name,' ENGINE=InnoDB;') FROM information_schema.tables WH ...