javascript原型,組合,動態原型,穩妥建構函式式

來源:互聯網
上載者:User

標籤:javascript原型   組合   動態原型   穩妥建構函式式   

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP ‘test3.jsp‘ starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script src="js/jquery-2.1.1.min.js"></script><script type="application/javascript">//原型的概念:原型對象裡的所有屬性和方法被所有建構函式執行個體化出來的對象所共用//function Person(){////}//Person.prototype={//constructor:Person,//name:‘程式員‘,//age:21,//friends:[‘wangwu‘,‘zhaoliu‘],//say:function(){//alert(this.friends);//}//}//原型裡的屬性和方法被所有對象共用/*var p1=new Person();p1.friends.push(‘lisi‘);p1.say();                        //wangwu,zhaoliu,lisivar p2=new Person();p2.say();                        //wangwu,zhaoliu,lisi ,p1添加friends,p2也添加了friends*///組合模式:一般使用原型和建構函式/*function Person1(name,age,friends,job){this.name=name;this.age=age;this.friends=friends;this.job=this.job;}Person1.prototype={constructor:Person1,sayName:function(){alert(this.name);}}var p3=new Person1(‘zhangsan‘,21,[‘wangwu‘,‘zhaoliu‘],‘程式員‘);var p4=new Person1(‘lisi‘,21,[‘wangwu‘,‘zhaoliu‘],‘程式員‘);p3.sayName();p4.sayName();*///動態原型模式(讓你的代碼封裝到一起)/*function Person2(name,age,friends,job){this.name=name;this.age=age;this.friends=friends;this.job=this.job;//動態原型方法if(typeof this.sayName !=‘function‘){//Person2.prototype.sayName=function(){          //sayName方法只建立一次alert(this.name);}}}var p5=new Person2(‘zhangsan‘,21,[‘wabgwu‘,‘zhaoliu‘],‘chengxuyuan‘);p5.sayName();*///穩妥建構函式式   durable object(穩妥對象) 非常安全的環境中//1.沒有公用屬性,//其他對象中也不引用this對象,不能使用this關鍵字function Person3(name,age,job){var object={};//可以定義私人的方法和屬性var name=name;var age=23;var job=‘程式員‘;object.sayName=function(){alert(name);}return object;}var p7=new Person3(‘zhangsan‘,21,‘chenuxyuan‘);p7.sayName();</script>   </head>    <body>    This is my JSP page. <br>  </body></html>


本文出自 “matengbing” 部落格,請務必保留此出處http://matengbing.blog.51cto.com/11395502/1879398

javascript原型,組合,動態原型,穩妥建構函式式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.