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

  • 【私有变量】 在对象内部使用’var’关键字来声明,而且它只能被私有函数和特权方法访问。

  • 【私有方法】 在对象的构造函数里声明(或者是通过varfunctionName=function(){…}来定义),它能被特权方法调用(包括对象的构造方法)和私有方法调用,私有函数只能访问私有的方法和属性。

  • 【特权方法】通过this.methodName=function(){…}来声明而且可能被对象外部的代码调用。它可以使用:this.特权函数() 方式来调用特权函数,使用 :私有函数()方式来调用私有函数。

  • 【公共属性】 通过this.variableName来定义而且在对象外部是可以读写的。不能被私有函数所调用。

  • 【公共方法】 通过ClassName.prototype.methodName=function(){…}来定义可以从对象外部来调用。

  • 【原型属性】 通过ClassName.prototype.propertyName=someValue 来定义。

  • 【静态属性】 通过ClassName.propertyName=someValue 来定义

  • 【静态方法】 通过ClassName.funName=function(){…} 来定义。


function myfun1(){
	//这是私有属性
	var private1 = "这是私有属性";
	var privateMethod = function(){
	   alert(private1);
	}
	//这是实例属性
	this.publicvar = "这是实例属性";
	this.public1 = function(){
	    privateMethod();
	}
}


var newfun1 = new myfun1();
newfun1.public1(); //这是私有属性
alert(newfun1.publicvar);//这是实例属性
alert(newfun1.private1); // undefined   newfun1.privateMethod(); //运行错误

function myfun2(){

}

myfun2.staticvar = "这是静态属性";
myfun2.staticmethod = function(){
   alert(myfun2.staticvar);
}

var newfun2 = new myfun2();
//newfun2.staticmethod();//运行错误;
alert(newfun2.staticvar);//undefined
//静态私有成员
var myfun3 = (function(){
   function privateProperty(){

   }
   privateProperty.staticvar = "这是静态私有成员";
   privateProperty.staticmethod = function(){
       alert(privateProperty.staticvar);
   }
    privateProperty.staticmethod();
    return privateProperty

 })();

//静态类
var funcount = 0;
var myfun4 = new function(){
   funcount++;
   this.printCount = function(){
       alert(funcount);
   }
}
myfun4.printCount(); //输出1;
myfun4.printCount(); //输出1;
myfun4.prototype.amethod = function(){
   alert("原型对象");
}//运行错误
var newfun4 = new myfun4();
newfun4.amethod();


//运行错误,说明myfun3创建并实例化之后就不能再为它添加方法,属性
new myfun3.constructor().printCount();//如果你确实想实例化,这样也可以.
//原型继承
var myfun5 = function(){

}
myfun5.prototype.myfun5_extend = function(){
   alert("这是原型继承的");
}
 var myfun5_sub = function(){

}
myfun5_sub.prototype = new myfun5();
var newfun5 = new myfun5_sub();
 newfun5.myfun5_extend(); //这是原型继承的
//调用继承
var myfun6 = function(){
   this.method_p = function(){
       alert("这是调用继承的");
   }
}
var myfun6_sub = function(){
   myfun6.call(this);
}

var newfun6 = new myfun6_sub();
newfun6.method_p();//这是调用继承的
//覆盖
var myfun7 = function(){
   this.method = function(){
       alert("这是父对象方法");
   }
}
var myfun7_sub = function(){
   this.method = function(){
       alert("这是子对象方法");
   }
}
myfun7_sub.prototype = new myfun7();
var newfun7 = new myfun7_sub();
newfun7.method(); //这是子对象方法,父对象方法被覆盖了.
//多态
function myfun8(a, b){
   var a = a;
   var b = b;
   if (typeof a == "number" && typeof b == "number") {
       alert(a * b);
   } else if (typeof a == "string" && typeof b == "string") {
       alert(a + b);
   } else {
       alert("输入错啦");
   }
}

myfun8(3, 4); // 输出12;
myfun8("hi,", "你好");//输出hi,你好;
myfun8("hi", 5);//输入错啦

原文链接:js–属性和方法(私有/公有)

**THE END**

猜你喜欢

转载自blog.csdn.net/Jioho_chen/article/details/84322071