还是以前的一贯风格,对于这种比较简单的就直接粘代码,同时大家也可以在线留言交流,我会及时回复的,当然,代码中的注释也是非常清楚的,相信大家也能看的非常明白,作为一个苦逼的程序员,还有比看代码更爽的事情吗,哈哈~
(function(){
//在javascript中我们利用function来定义类
function Shape(){
var x = 1;
var y = 2
}
//然我们如何实例化一个对象呢? 通过new 关键字
var aShape = new Shape();
//在类的内部我们用var 定义的是私有变量 如何才能定义共有变量呢?
function Shape2(){
this.x = 1;
this.y = 2;
}
var bShape = new Shape2();
//测试
//alert(bShape.x)
//处理定义私有变量外还可以用var定义私有函数
//private 函数
function Shape3(){
var draw = function(){
//私有函数
}
this.draw2 = function(){
//外界可以看到的共有函数
}
}
var c = new Shape3();
c.draw2();
//用javascript模仿OOP编程
function Shape4(ax,ay){
var x = 0;
var y = 0;
var init = function(){
x = ax;
y = ay;
}
init();
this.getX = function(){
return x;
}
}
var d = new Shape4(2,4);
alert(d.getX());
//模仿OOP编程的构造函数,现在我们来写静态属性和静态方法
//JS中静态方法是作用到类身上的而非是对象
function Person(){this.Name = "YUNFENGCHENG"};
//静态变量
Person.age = 0;
Person.showName = function(obj){
alert(obj.Name)
}
Person.showName(new Person())
// Array.each= function(){
// }
//简单类定义方法
var a = {};
var array = [];
a["name"] = "USPCAT.COM";
alert(a.name)
})()
/**
* Map
*/
(function(){
function jMap(){
//私有变量
var arr = {};
//增加
this.put = function(key,value){
arr[key] = value;
}
//查询
this.get = function(key){
if(arr[key]){
return arr[key]
}else{
return null;
}
}
//删除
this.remove = function(key){
//delete 是javascript中关键字 作用是删除类中的一些属性
delete arr[key]
}
//遍历
this.eachMap = function(fn){
for(var key in arr){
fn(key,arr[key])
}
}
}
var country = new jMap();
country.put("01","ZG");
country.put("02","HG");
country.put("03","MG");
country.put("04","TG");
//alert(country.get("01"))
country.remove("01")
//alert(country.get("01"))
country.eachMap(function(key,value){
document.write(key+"-->"+value+"<br>");
})
})()