<!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. nyoj 711 枚举+并查集

     #include<stdio.h>//从大到小不断枚举边直到找到s-t的路径,判断从s可以到t可以用并查集来判断 #include<stdlib.h>//枚举最大的一条边肯定 ...

  2. magento 的一些关于addFieldToFilter的查询

    1,匹配country_id的首字母,查询国家,返回数组 //查询国家数据集 $countryCollection=Mage::getResourceModel('directory/country_ ...

  3. 手动重启weblogic脚本

    手动重启weblogic脚本 pid=`ps -ef|grep fzjc_Admin_Server|grep -v grep|awk '{print $2}'` echo $pid kill -9 $ ...

  4. codechef Little Elephant and Bombs题解

    The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...

  5. android app 架构设计01

    1:本文有摘抄, 1 2 3 4 5 - 开发过程中.需求.设计.编码的一致性 - 整个程序具有统一的风格,比方对话框样式,button风格,色调等UI元素 - 整个程序详细统一的结构,比方不同模块訪 ...

  6. 2016.04.25,英语,《Vocabulary Builder》Unit 18

    capit, from the Latin word for 'head', caput ['keɪpət] n.头,首 , turns up in some pretty important pla ...

  7. luogu1771 方程的解

    题目大意 对于不定方程a1+a2+…+ak-1+ak=g(x),其中k≥2且k∈N,x是正整数,g(x)=x^x mod 1000(即x^x除以1000的余数),x,k是给定的数.我们要求的是这个不定 ...

  8. Swift - 获取当前时间的时间戳(时间戳与时间互相转换)

    (本文代码已升级至Swift3) 1,时间戳 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. 2,获取当前时间的时 ...

  9. thinkphp 具体常量,在view里面使用

    1 2 3 4 5 6 7 8 9 '__TMPL__'      =>  APP_TMPL_PATH,  // 项目模板目录 '__ROOT__'      =>  __ROOT__,  ...

  10. [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)

    interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...