js里面的属性和方法类型

js--属性和方法(私有/公有)

【私有变量】 在对象内部使用'var'关键字来声明,而且它只能被私有函数和特权方法访问。 
【私有方法】 在对象的构造函数里声明(或者是通过varfunctionName=function(){...}来定义),
它能被特权方法调用(包括对象的构造方法)和私有方法调用,私有函数只能访问私有的方法和属性。 
【特权方法】通过this.methodName=function(){...}来声明而且可能被对象外部的代码调用。
它可以使用:this.特权函数() 方式来调用特权函数,使用 :私有函数()方式来调用私有函数。

【公共属性】 通过this.variableName来定义而且在对象外部是可以读写的。不能被私有函数所调用。 
【公共方法】 通过ClassName.prototype.methodName=function(){...}来定义可以从对象外部来调用。 
【原型属性】 通过ClassName.prototype.propertyName=someValue 来定义。 
【静态属性】 通过ClassName.propertyName=someValue 来定义。
【静态方法】 通过ClassName.funName=function(){...} 来定义。

1 function  myfun1(){
2    //这是私有属性
3    var  private1 = "这是私有属性" ;
4    var  privateMethod = function (){
5        alert(private1);
6    }
7    //这是实例属性
8    this .publicvar = "这是实例属性" ;
9    this .public1 = function (){
10        privateMethod();
11    }
12 }
 
1 var  newfun1 = new  myfun1();
2 newfun1.public1(); //这是私有属性
3 alert(newfun1.publicvar); //这是实例属性
4 alert(newfun1.private1); // undefined   newfun1.privateMethod(); //运行错误
 
1 function  myfun2(){
2 }
3 myfun2.staticvar = "这是静态属性" ;
4 myfun2.staticmethod = function (){
5    alert(myfun2.staticvar);
6 }
7 var  newfun2 = new  myfun2();
8 //newfun2.staticmethod();//运行错误;
9 alert(newfun2.staticvar); //undefined
 
1 //静态私有成员
2 var  myfun3 = ( function (){
3    function  privateProperty(){
4   
5    }
6    privateProperty.staticvar = "这是静态私有成员" ;
7    privateProperty.staticmethod = function (){
8        alert(privateProperty.staticvar);
9    }
10    privateProperty.staticmethod();
11    return  privateProperty
12 })();
1 //静态类
2 var  funcount = 0;
3 var  myfun4 = new  function (){
4    funcount++;
5    this .printCount = function (){
6        alert(funcount);
7    }
8 }
9 myfun4.printCount(); //输出1;
10 myfun4.printCount(); //输出1;
11 myfun4.prototype.amethod = function (){
12    alert( "原型对象" );
13 } //运行错误
14 var  newfun4 = new  myfun4();
15 newfun4.amethod();
 
 
1 //运行错误,说明myfun3创建并实例化之后就不能再为它添加方法,属性
2 new  myfun3.constructor().printCount(); //如果你确实想实例化,这样也可以.
3 //原型继承
4 var  myfun5 = function (){
5
6 }
7 myfun5.prototype.myfun5_extend = function (){
8    alert( "这是原型继承的" );
9 }
10 var  myfun5_sub = function (){
11
12 }
13 myfun5_sub.prototype = new  myfun5();
14 var  newfun5 = new  myfun5_sub();
15 newfun5.myfun5_extend(); //这是原型继承的
16 //调用继承
17 var  myfun6 = function (){
18    this .method_p = function (){
19        alert( "这是调用继承的" );
20    }
21 }
22 var  myfun6_sub = function (){
23    myfun6.call( this );
24 }
25
26 var  newfun6 = new  myfun6_sub();
27 newfun6.method_p(); //这是调用继承的
28 //覆盖
29 var  myfun7 = function (){
30    this .method = function (){
31        alert( "这是父对象方法" );
32    }
33 }
34 var  myfun7_sub = function (){
35    this .method = function (){
36        alert( "这是子对象方法" );
37    }
38 }
39 myfun7_sub.prototype = new  myfun7();
40 var  newfun7 = new  myfun7_sub();
41 newfun7.method(); //这是子对象方法,父对象方法被覆盖了.
42 //多态
43 function  myfun8(a, b){
44    var  a = a;
45    var  b = b;
46    if  ( typeof  a == "number"  && typeof  b == "number" ) {
47        alert(a * b);
48    } else  if  ( typeof  a == "string"  && typeof  b == "string" ) {
49        alert(a + b);
50    } else  {
51        alert( "输入错啦" );
52    }
53 }
54
55 myfun8(3, 4); // 输出12;
56 myfun8( "hi," , "你好" ); //输出hi,你好;
57 myfun8( "hi" , 5); //输入错啦.

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值