js的对象(oop)

jquery是一种封装js的技术,能用简洁的代码来完成完美的效果。
下面我来学习下jsoop来打下坚实的基础
[b]1./**
* 定义函数的方式定义类
*/[/b]
function persion() {
// 类成员的定义与构造函数
this.age = 123;
}


// 实例化persion对象
var p = new persion();


//获取属性的两种方式
// alert(p["age"]); 这个主要是调用属性的方法
// alert(p.age);
[b]2.[/b]

// 动态添加,修改,删除对象的属性和方法
// 添加属性
p.sex = "male";
//alert(p.sex);


// 添加方法
p.getSex = function() {
return this.sex;
};
//alert(p.getSex());
// 修改属性
p.sex = "female";
// alert(p.sex);


// 删除属性
p.sex = undefined;
//alert(p.sex);


// 删除方法
p.getSex = undefined;
//alert(p.getSex());


// ---------------------------------------------------


[b]3[/b]./**
* 使用{}的语法创建无类型对象
*/
// 定义一个空对象
var obj = {};


var man = {


name : "fy",// 定义了name属性,初始化为fy


favoriteColor : ['black', 'red', 'green'],


sayHello : function() {// 定义了方法sayHello
alert("hello world!!");
}
};
//调用man中的sayHello方法
man.sayHello();

//我的理解是{}可以创建一个类似java的类,在里面可以定义 属性 EX:name, 函数 sayHello:function(){}


[b]4.[/b]
/**
* protoType原型对象
*/


//定义一个空对象
function woman(){
//this is empty
}

//对类的prototype对象进行修改,增加方法getSex();
woman.prototype.getSex = function(){
alert("I'm a woman");
};

对这个空的方法进行修改 方式是 woman.prototype.getSex=function(){};
//实例化woman类
var w = new woman();//得到一个object实例化对象 通过这个对象可以调用方法和属性
//w.getSex();


// ---------------------------------------------------


[b]5[/b]./**
* 实现类的私有成员
*/
function boy(){
var pp = "peope are private萧黎私有";//私有属性成员pp
//定义一个名字为pm的函数 我觉得那个var就是相当与java中的private
function pm(){//私有方法成员pm
alert(pp);
}
this.getPp = function(){//通过this. 你将会将私有的变成公有的
//在公有成员中改变私有属性的值
pp = "peope are private???公有的";
alert(pp);
},
//这样是定义一个共有的,但是和私有的不冲突。
this.getPm = function(){
//在公有属性中调用私有方法
pm();
};
}
每次都是通过实例化后得到object对象后才能调用其方法和属性
//实例化boy类
var boy = new boy();
// boy.getPm();
// boy.getPp();
//boy.getPm();


/**
[b]6[/b]. * 实现静态成员(属性和方法)
*/


function girl(){
}


//静态属性 与以往添加属性不同的是,这是通过函数.属性=“”;
//他的好处相对与java而言属性和方法并不需要事先声明和定义。
girl.Property = "我是静态属性class level";


//静态方法
girl.staticMethod = function(){
alert("static method!!!");
};


//调用静态属性与方法
//alert(girl.Property);因为这个属性是静态的所以不需要得到对象才可以调用我们可以直接的调用
// girl.staticMethod(); //同上


[b]7.[/b]/**
* 利用反射机制和prototype实现继承
*/
function father11(){


}
father1.prototype = {
method1:function(){
alert(1);
},
method2:function(){
alert(2);
}
};


function child11(){
alert(child1);
}


//让child1继承father1
for(var p in father11.prototype){
//alert(p);
child11.prototype[p] = father11.prototype[p];
}
//覆盖定义father1中的method1方法
child11.prototype.method1 = function(){
alert(11);
};
//创建两个类的实例
var f1 = new father11();
var c1 = new child11();
//分别调用两个类的method1方法
f1.method1();
c1.method1();

// ---------------------------------------------------
[b]
8
[/b]
// ---------------------------------------------------
/**
* 简化版的利用反射机制和prototype实现继承
*/
function father2(){


}


//为类添加静态方法inherit表示继承与某类
//继承1
// Function.prototype.inherit = function(baseClass){
// for(var p in baseClass){
// this.prototype[p] = baseClass.prototype[p];
// }
// };
//继承2
Function.prototype.extend = function(baseClass){
for(var p in baseClass){
this.prototype[p] = baseClass.prototype[p];
}
};


father2.prototype = {


method1:function(){
alert(father21);
},
method2:function(){
alert(father22);
}
};


function child2(){
}
//继承3
//让child2继承father2
// for(var p in father2.prototype){
//
// child2.prototype[p] = father2.prototype[p];
// }\
//child2.inherit(father2);
child2.extend(father2);


//覆盖定义father1中的method1方法
child2.prototype.method = function(){
alert(11);
};


//创建两个类的实例
var f2 = new father2();
var c2 = new child2();
//分别调用两个类的method1方法
f2.method1();
c2.method();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值