<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript面向对象知识复习</title>
</head>
<body>
<h2></h2>
<script type="text/javascript">
/************************************************类,成员属性,成员方法******************************************************/
/**
* 定义一个类
* @param name
* @param age
* @constructor
*/
function MyClass(name, age) {
this.name = name;
this.age = age; // 成员方法
this.toString = function () {
alert(cls1.name+":"+cls1.age);
};
}; /**
* 实例化一个cls1对象
* @type {MyClass}
*/
var cls1 = new MyClass("xiugang", 15);
//alert(cls1.name+":"+cls1.age);
cls1.toString(); // 再给这个类的一个对象cls2添加一个方法
var cls2 = new MyClass("zhangsan", 25);
cls2.ShowName = function () {
alert(this.name+":"+this.age);
};
cls2.ShowName(); // 使用Prototype对象来给函数添加方法
function Animal(name, age) {
this.name = name;
this.age = age;
} Animal.prototype.toString = function () {
alert(this.name+":"+this.age);
}; // 实例化两个对象
var dog = new Animal("dog", 15);
dog.toString();
var cat = new Animal("cat", 16);
cat.toString(); // 利用prototype属性给一个类添加多个方法
function Person(name, age) {
this.name = name;
this.age = age;
};
Person.prototype = {
toString : function () {
alert(this.name+":"+this.age);
},
sayHello : function () {
alert("say Hello!");
} }; var student = new Person("小明", 25);
student.sayHello();
student.toString(); /************************************************静态类******************************************************/
var StaticClass = function () { }
StaticClass.name = "StaticClass";
StaticClass.Sum = function (value1, value2) {
return value1 + value2;
};
alert(StaticClass.name+", "+StaticClass.Sum(10, 20)); /************************************************继承******************************************************/
function PeopleClass() {
this.type = "People";
};
PeopleClass.prototype = {
getType : function () {
alert("This is a Person");
}
}; function StudentClass(name, sex) {
// 使用apply方法将父类对象的构造函数绑定到子类对象上
PeopleClass.apply(this, arguments);
this.name = name;
this.sex = sex;
}
var stu = new StudentClass("小红", "女");
alert(stu.type); // 实现了属性的继承 /**
* 实现方法的继承
*/
function Sophermore(name, sex) {
PeopleClass.apply(this, arguments);
// 实现父类方法的继承
/**
* 实现思路: 需要循环将父类对象的prototype进行赋值, 即可达到继承的目的
*/
var prop;
for (prop in PeopleClass.prototype){
var proto = this.constructor.prototype;
if (!proto[prop]){
proto[prop] = PeopleClass.prototype[prop];
}
proto[prop]["super"] = PeopleClass.prototype;
}
this.name = name;
this.sex = sex;
}
var stu2 = new Sophermore("xiuxiu", 22);
alert(stu2.type);
stu2.getType() /**
* 方法二:实现继承的第二种方法, 使用对象冒充的方法
*/
function AnimalNew(name, age) {
this.name = name;
this.age = age; this.Sum = function () {
alert(this.name+","+this.age);
}
}
// 成员方法
/*AnimalNew.prototype = {
sayhello : function () {
alert(this.name+"is saying Hello!");
},
sayAge : function () {
alert(this.name+"'s age is "+this.age);
}
}*/
AnimalNew.prototype.sayHello = function () {
alert(this.name+" is saying Haha!");
} // 子类开始实现继承
function Duck(name, age) {
this.animal = AnimalNew; this.animal(name, age);
} var duck = new Duck("鸭子", 12);
//duck.sayHello(); //error!
//duck.sayAge(); //error!
//duck.sayHello(); //error!
duck.Sum(); //ok的! /************************************************JavaScript继承知识加强******************************************************/
function Animal(name) {
// 属性
this.name = name; //实例方法
this.sleep = function () {
console.log(this.name+"正在睡觉!");
}
}
// 原型方法
Animal.prototype.eat = function (food) {
console.log(this.name+"正在吃"+food);
} /**
* 方法一: 将父类的实例作为子类的原型, 可以同时实现父类的属性和方法的继承
*/
function Cat() { }
Cat.prototype = new Animal();
Cat.prototype.name = "cat"; // test
var cat =new Cat();
console.log(cat.name);
cat.sleep()
cat.eat("fish");
console.log(cat instanceof Animal);
console.log(cat instanceof Cat); /**
* 方法二: 组合继承
* 通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用
*/
function Cow(name) {
Animal.call(this);
this.name = name;
}
Cow.prototype = new Animal();
Cow.prototype.constructor = Cat; var cow = new Cow("小牛博客");
console.log(cow.name);
console.log(cow.sleep());
console.log(cat instanceof Animal);
console.log(cat instanceof Cat); // 利用方法二:组合继承实现继承的综合案例
function Family(name, age) {
// 属性
this.name = name;
this.age = age; // 实例方法
this.Member = function () {
alert("This family is having 5 memnbers now!");
}
}; // 原型方法
Family.prototype = {
sayHello : function () {
alert(this.name +" is saying hello!");
},
sayAge : function () {
alert(this.name +" is saying age:"+this.age);
}
}; // 开始实现继承
function Son(name, age) {
Family.call(this); this.name = name;
this.age = age;
}
Son.prototype = new Family();
Son.prototype.constructor = Family; // 开始测试
var son = new Son("王老大", 15);
alert(son.age+", "+son.age);
son.sayAge();
son.sayHello();
alert(son instanceof Family);
alert(son instanceof Son); </script>
</body>
</html>

JavaScript进阶【三】JavaScript面向对象的基础知识复习的更多相关文章

  1. PHP基础入门(五)---PHP面向对象实用基础知识

    前言: 今天来和大家介绍一下PHP的面向对象.说到面向对象,我不得不提一下面向过程,因为本人在初学时,常常分不清楚面向对象和面向过程,下面就来给大家介绍一下它们的区别: 面向对象专注于由哪个对象来处理 ...

  2. Objective-C 基础教程第三章,面向对象编程基础知

    目录 Objective-C 基础教程第三章,面向对象编程基础知 0x00 前言 0x01 间接(indirection) 0x02 面向对象编程中使用间接 面向过程编程 面向对象编程 0x03 OC ...

  3. Python学习-第三天-面向对象编程基础

    Python学习-第三天-面向对象编程基础 类和对象 简单的说,类是对象的蓝图和模板,而对象是类的实例.这个解释虽然有点像用概念在解释概念,但是从这句话我们至少可以看出,类是抽象的概念,而对象是具体的 ...

  4. 沙朗javascript总结一下(一)---基础知识

    我也听说过Javascript这东西.我一直感觉很神奇,但它并没有去太懂.今天,牛腩哥哥随后的初步研究,一些浅显的认识.就先总结一下. 首先,什么是javascript? javascript是一种直 ...

  5. 《JAVASCRIPT高级程序设计》表单基础知识和文本框脚本

    在HTML中,表单是由<form>元素来表示,在javascript中,表单对应的是HTMLFormElement类型,它具有一些独有的属性和方法: 一.表单基础知识 1.取得表单的方式 ...

  6. JavaScript基础知识复习

    1,javascript是基于对象和事件驱动的,并有安全性能的脚本语言: 2,javascript的特点: 1)向HTML中添加交互事件: 2)脚本语言,与java语法类似: 3)解释性语言,边执行边 ...

  7. 【进阶之路】Redis基础知识两篇就满足(一)

    导言 大家好,我是南橘,一名练习时常两年半的java练习生,这是我在博客园的第一篇文章,当然,都是要从别处搬运过来的,不过以后新的文章也会在博客园同步发布,希望大家能多多支持^_^ 这篇文章的出现,首 ...

  8. 【进阶之路】Redis基础知识两篇就满足(二)

    导言 大家好,我是南橘,一名练习时常两年半的java练习生,这是我在博客园的第二篇文章,当然,都是要从别处搬运过来的,不过以后新的文章也会在博客园同步发布,希望大家能多多支持^_^ 这篇文章的出现,首 ...

  9. Golang 入门系列(三)Go语言基础知识汇总

    前面已经了 Go 环境的配置和初学Go时,容易遇到的坑,大家可以请查看前面的文章 https://www.cnblogs.com/zhangweizhong/category/1275863.html ...

随机推荐

  1. Windows 2003 IIS 不支持ASP的问题

    Windows 2003 IIS 不支持ASP的问题 问题: HTTP 错误 404 - 文件或目录未找到. Internet 信息服务 (IIS) 第一步,启用Asp,进入:控制面板 -> 管 ...

  2. MySQL:浅析 Impossible WHERE noticed after reading const tables

    使用 EXPLAIN 执行计划的时候,在 Extra 中偶尔会看到这样的描述: Impossible WHERE noticed after reading const tables 字面上的意思是: ...

  3. 【ACM】poj_3981_字符串替换_201307271019

    字符串替换Time Limit: 1000MS  Memory Limit: 65536K Total Submissions: 8447  Accepted: 3988 Description 编写 ...

  4. P2310 loidc,看看海

    P2310 loidc,看看海 题目背景 loidc喜欢大海.在他放假的时候他经常一个人跑到海边独自玩耍. 在浪花的冲击下,他可以忘记打代码的烦躁,真是惬意极了. 虽然今天是周六,但今天可是11.8号 ...

  5. TRIZ系列-创新原理-8-重量补偿原理

    重量补偿原理的表述例如以下: 1)将某一物体与还有一种提供上升力的物体组合,以补偿其重量:2)通过与环境(利用空气动力,流体动力或其他力等)的相互作用.实现对物体的重量补偿: 重力使得我们能够稳稳的依 ...

  6. Python——迭代器和解析(3)

    用迭代工具模拟zip和map ====================================================================== 我们已经知道了zip怎样组合 ...

  7. Selenium webdriver-UI Element定位

    转:http://blog.csdn.net/jillliang/article/details/8206402 1.创建Fixfox web driver实例 WebDriver driver =  ...

  8. wpf datagridtemplatecolumn visibility binding

    因为datagridtemplatecolumn不在Virsual Tree中,不能继承DataGrid的DataContext, 所以想要绑定到datagridtemplatecolumn的 vis ...

  9. 深入理解 C 指针阅读笔记 -- 第二章

    Chapter2.h #ifndef __CHAPTER_2_ #define __CHAPTER_2_ /*<深入理解C指针>学习笔记 -- 第二章*/ /* 内存泄露的两种形式 1.忘 ...

  10. Working with SQL Server LocalDB

    https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html The ApplicationDbContex ...