现在的位置: 首页 > 综合 > 正文

JavaScript一般定义形式

2013年09月24日 ⁄ 综合 ⁄ 共 601字 ⁄ 字号 评论关闭
                // A: 定义JS Function
		var c = function(){}
		function c(){}
		
		// B: JS Function添加属性和方法
		function c(){
			// private属性
			var p1 = "p1";
			// public 属性		 		
			this.p2 = "p2";        		
			
			// private方法,只能在内部使用,只能访问私有属性
			function m1(){				
				alert("m1");
			}
			//public 方法
			this.m2 = function(arg0){
				alert("m2");
			}
		}
		// static 方法
		c.staticM = function(){			
		}
		// 通过 prototype 添加属性和方法(N1:一种继承方式)	
		c.prototype = {		
			ab: "ab",
			ac: function(){
				alert("ac");
			},
			ad: function(arg0){
				alert("ad");
			}
		}
		// 通过 prototype 添加一个public方法
		c.prototype.aa =  function(arg0){
			alert("aa");
		}

		// C: 继承一个 JS
		function c2() {
		}
		// 继承 function c(N1 N2不能同时存在,只能单继承)	
		c2.prototype = new c();
		// 扩展(N3:要先继承,再扩展,顺序反了之后会无效,)
		c2.prototype.m = function(arg0){
			alert("c2");
		}
		c2.prototype.p1 = 0;

抱歉!评论已关闭.