您的位置:首页 > Web前端 > JavaScript

Javascript对象学习2 - 继承

2013-10-16 14:00 363 查看
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Javascript的继承的简单学习</title>
</head>
<body>

<script type="text/javascript">

(function(){

/**
* Javascript的继承的简单学习
*
* 1.对象冒充方式
*/
/*
//被继承对象
function User(name) {
this.name = name;
this.sayName = function() {
alert(this.name);
}
}
function Student(name,age) {
this.temp = User;
this.temp(name);
//删除对User的引用,确保后续代码不能再次通过temp操作User,要不this.temp("aa");还会起作用
//delete this.temp;
//this.temp("aa");

//新属性和新方法都必须在删除新方法的代码行后定义。否则,可能会覆盖父类相关的属性和方法
//this.name = "bb";
this.age = age;
this.showAge = function() {
alert(this.age);
}
}
//调用
var stu1 = new Student("tom",23);
stu1.sayName();
stu1.showAge();
*/
/**
* 1.1.继承:对象冒充方式(多重继承)
*/

/*          //被继承对象1
function User(name) {
this.name = name;
this.sayName = function() {
alert(this.name);
}
}
//被继承对象2
function Like(item) {
this.item = item;
//  this.name = "";
this.play = function() {
alert(this.item);
}
}
function Student(name,age,item) {
this.user = User;
this.user(name);
delete this.user;
this.like = Like;
this.like(item);
delete this.like;
this.age = age;
this.showAge = function() {
alert(this.age);
}
}
//调用
var stu1 = new Student("Rose",25,"basketball");
stu1.sayName();
stu1.showAge();
stu1.play();
//缺点:如果存在两个类User和Like具有同名的属性或者方法,Like具有更高的优先级

*/
/**
* 2.继承:使用call()方法
*/
/*          function show(age,item) {
alert(this.name + " --- " + age + " --- " + item);
}
var obj = new Object();
obj.name = "tom";
//调用
show.call(obj,21,"football"); //第一个参数最好不要少
//相当于:obj.show = show(); → show(21,"football")
*/
/**
* 2.1 继承:call()方法继承,多重继承
*/
/*          //被继承对象1
function User(name) {
this.name = name;
this.sayName = function() {
alert(this.name);
}
}
//被继承对象2
function Like(item) {
this.item = item;
//  this.name = "";
this.play = function() {
alert(this.item);
}
}
function Student(name,age,item) {
User.call(this,name);
Like.call(this,item);
this.age = age;
this.showAge = function() {
alert(this.age);
}
}
//调用
var stu1 = new Student("Rose",25,"basketball");
stu1.sayName();
stu1.showAge();
stu1.play();
alert(stu1 instanceof User);//false
alert(stu1 instanceof Like);//false
alert(stu1 instanceof Student);//true
alert(stu1.constructor); //function Sudent()....
*/
/**
* 3.继承:apply()方法
*/

/*          //被继承对象1
function User(name) {
this.name = name;
this.sayName = function() {
alert(this.name);
}
}
//被继承对象2
function Like(item) {
this.item = item;
//  this.name = "";
this.play = function() {
alert(this.item);
}
}
function Student(name,age,item) {
User.apply(this,[name]);//变化之处
Like.call(this,[item]);
this.age = age;
this.showAge = function() {
alert(this.age);
}
}
//调用
var stu1 = new Student("Rose",25,"basketball");
stu1.sayName();
stu1.showAge();
stu1.play();
alert(stu1 instanceof User);//false
alert(stu1 instanceof Like);//false
alert(stu1 instanceof Student);//true
alert(stu1.constructor); //function Sudent()....
*/
/**
* 4.继承:原型连方式
*/

//被继承对象
/*          function User() {}
User.prototype.name = "";
User.prototype.sayName = function() {
alert(this.name);
};

function Student() {}
//①将User中所有通过prototype设置的属性或方法放到Student中,并覆盖了Student中所有的prototype设置。所以应该放在第一行
Student.prototype = new User();
Student.prototype.age = 0;
Student.prototype.showAge = function() {
alert(this.age);
}
//Student.prototype = new User(); //一定要放在第一行哦
var stu = new Student();
stu.name = "tom";
stu.age = 23;
stu.sayName();
stu.showAge();
alert(stu instanceof User);//true
alert(stu instanceof Student);//true
alert(stu.constructor); //function User()....
//缺点:不支持多重继承,构造函数不能有参数
*/
/**
* 5.继承:混合模式 = 构造函数 + 原型链方式
*/
/*           function User(name) {
this.name = name;
}
User.prototype.sayName = function() {
alert(this.name);
}
function Student(name,age) {
//User.call(this,name);
this.age = age;
}
//将User中所有通过prototype设置的方法放到Student中
Student.prototype = new User();
Student.prototype.showAge = function() {
alert(this.age);
}
//调用
var stu = new Student();
stu.name = "tom";
stu.age = 23;
stu.sayName();
stu.showAge();
alert(stu instanceof User);//true
alert(stu instanceof Student);//true
alert(stu.constructor); //function User(name) {this.name = name;}
*/
function User(name) {
this.name = name;
}
User.prototype.sayName = function() {
alert(this.name);
}
function Student(name,age) {
//User.call(this,name);
//this.age = age;
}
//将User中所有通过prototype设置的方法放到Student中
Student.prototype = new User();
Student.prototype.age = 0;
Student.prototype.showAge = function() {
alert(this.age);
}
//调用
var stu = new Student();
stu.name = "tom";
stu.age = 23;
stu.sayName();
stu.showAge();
alert(stu instanceof User);//true
alert(stu instanceof Student);//true
alert(stu.constructor); //function User(name) {this.name = name;}
})();

</script>
</body>
</html>


本文出自 “我的JAVA世界” 博客,请务必保留此出处http://hanchaohan.blog.51cto.com/2996417/1309679
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: