(function(){
//在js中我们利用function来定义类
function Shape(){
//在类的内部使用var声明的为私有变量
var x = 1;
var y = 1;
};
//利用new关键字进行类的实例化
var oShape = new Shape();
console.log(oShape.x);//undefined,类的实例化对象中不能访问到私有变量
//如何在类中定义公有变量
function Parent(){
this.x = 1;
this.y = 1;
};
var oParent = new Parent();
console.log(oParent.x); //1,在类中使用this关键字来定义公有变量,可以在类的实例化对象中访问到
//定义私有函数
function Box(){
//私有函数
var draw1 = function(){
alert("draw1")
};
//公有函数
this.draw2 = function(){
alert("draw2")
};
};
var oBox = new Box();
// oBox.draw1(); //报错
oBox.draw2(); //draw2
})();
(function(){
//用javascript模仿OOP编程,为什么需要使用OOP编程,因为能够模块化,结构化,方便一个团队共同开发,但是在javascript中没有类的概念,只能模仿
function Slider(x,y){
var x = 0;
var y = 0;
var init = function(){
x = x;
y = y;
};
init();
};
var oSlider = new Slider();
})();