<!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. 【ACM】bailian_2705_跳绳游戏_201307302003

    2705:跳绳游戏总时间限制: 1000ms 内存限制: 65536kB 描述 小朋友玩跳绳比赛,要计算在一分钟内跳了多少下.假设每秒钟跳一下,如果中途失败了,则要花三秒钟后才能开始重跳.一般小朋友跳 ...

  2. 洛谷——P1910 L国的战斗之间谍

    https://www.luogu.org/problem/show?pid=1910#sub 题目背景 L国即将与I国发动战争!! 题目描述 俗话说的好:“知己知彼,百战不殆”.L国的指挥官想派出间 ...

  3. EditText焦点问题

    1.在一个Activity中加入一个EditText后,每次进入这个Activity时输入法都会自己主动弹出来.非常烦,找了些资料,在此记下解决的方法: 方法:在EditText的父控件中获得焦点.这 ...

  4. 自己主动化的在程序中显示SVN版本号

    有时候会有这种情况,策划拿着应用过来提一个bug,但我们却不好确定策划的手机上装的应用相应的是那个代码版本号. 为了解决问题.我们希望能在应用上显示出当前应用所相应的代码版本号,即svn版本号. 构想 ...

  5. 用Java做的类似皇家守卫战的游戏

    最近因为数据结构的课设缘故,所以用Java做了一款类似皇家守卫战(本人最钟情的一款PC兼手游的塔防游戏)的游戏.现在把这个游戏放出来,可以下载下来 玩耍 学习,代码中我也做了大量的注释.(运行游戏得带 ...

  6. 分享一个iOS输入框特殊限制的代码 UITextField (Validation)

    //个人总结.欢迎新增或改动 #import <UIKit/UIKit.h> typedef enum{ VALIDATION_TYPE_NUM_VALIDATED = 0,//数字 VA ...

  7. Linux命令(六)——软件包管理(安装应用程序)

    与windows安装各种应用程序相似,在linux下也可以安装各种需要的应用程序,通常称为软件包.目前,在linux系统下常见的软件包格式主要有:RPM包.TAR包.bz2包.gz包.deb包.sh结 ...

  8. crm高速开发之OrganizationService

    这是主要的开发模式: /* 创建者:菜刀居士的博客  * 创建日期:2014年07月06号  */ namespace Net.CRM.OrganizationService {     using ...

  9. Dragon Ball--hdoj

    Dragon Ball Problem Description Five hundred years later, the number of dragon balls will increase u ...

  10. [SPOJ 30669] Ada and Trip

    [题目链接] https://www.spoj.com/problems/ADATRIP/ [算法] 直接使用dijkstra堆优化算法即可 [代码] #include<bits/stdc++. ...