JavaScript面向对象编程

一面向对象编程的特性

  1  封装性;

  2  抽象性;

  3  继承性;

  4  多态性;

二javaScript实现数据的封装

说明:javaScrpt利用局部变量的特性来实现数据的封装(成员的私有化)

深入理解全局变量和局部变量的区别可参考:http://blog.csdn.net/zyz511919766/article/details/7276089

function Person (){
	//定义私有变量
	var name ="";
	var age =0;
	
	this.getName = function(){
		return this.name;//必须添加this,返回的才上成员变量
	}
	this.getAge = function(){
		return this.age;
	}
	this.setName = function(name){
		this.name = name;
	}
	this.setAge = function(age){
		this.age = age;
	}
}

var person  = new Person();
person.setName("lin");
person.setAge(26);
document.write("name:"+person.getName()+",age:"+person.getAge());

三javaScript实现继承

  1利用原型对象实现继承

说明:每个函数对象都具有prototype属性,该属性保存当前函数对象的原型对象的引用。函数对象的所以属性 和方法

都继承这个原型对象。

function Person(name,sex){
	this.name =name;
	this.sex  =sex;
	
}

Person.prototype.sayHello =function(){
	document.write("hello,everybody,"+this.name+"<br>");
}

function Student(){
	
}

/*
extends Person,获得父类所有的属性和方法,其方法共享一个副本,因为方法是其原型的。
*/
Student.prototype = new Person("jjlin","man");

var student = new Student();

Student.prototype.grade ="three";
Student.prototype.introduce = function(){
	document.write("i am "+this.grade+"的学生!");
}

student.sayHello();
student.introduce();

2利用构造函数实现继承

function ClassB(name){
	this.name = name;
	
}
ClassB.prototype.sayHello = function(){
		document.write("hello,my name is"+this.name+"<br>");
	}
	
function ClassA(name,age){
	this.extendsMethod= ClassB;//将ClassB作为ClassA的方法
	this.extendsMethod(name);//调用父类的方法,即构造函数
	delete this.extendsMethod;
	
	this.age = age;
}
ClassA.prototype.sayHello = function(){
	document.write("hello,my name is"+this.name+",age:"+this.age+"<br>");
}

var classB = new ClassB("jjlin");
classB.sayHello();

var classA = new ClassA("lin","26");
classA.sayHello();

3使用call()和apply()方法实现继承

function Animal(color,age){
	this.color =color;
	this.age = age;
	
	this.cry = function(){
		document.write("Animal-->color:"+this.color+",age:"+this.age+"<br>");
	}
}

function Dog(color,age){
	//使用call()方法,将Animal函数绑定到Dog实例上执行
	Animal.call(this,color,age);
}

function Cat(color,age){
	Animal.apply(this,[color,age]);
}

var animal = new  Animal("yellow",5);
animal.cry();

var dog = new Dog("black",4);
dog.cry();
var cat = new Cat("white",5);
cat.cry();

4使用混合方式(构造函数定义属性,采用原型定义方法,apply,call方法,实现继承)

function Parent(hello){
	this.hello = hello;

}

Parent.prototype.sayHello = function(){
	document.write("sayHello----->"+this.hello+"<br>");
}

function Child(hello,world){
	Parent.call(this,hello);//继承父类的构造函数
	this.world = world;
}

Child.prototype = new Parent();//实现方法的继承

Child.prototype.sayWorld = function(){
	document.write("sayWorld--->"+this.world+"<br>");
}

var child = new Child("hello","world");
child.sayHello();
child.sayWorld();



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值