js实现继承的五种方式
function Parent(firstname)
{
this.fname=firstname;
this.age=;
this.sayAge=function()
{
console.log(this.age);
}
}
function Child(firstname)
{
this.parent=Parent;
this.parent(firstname);
delete this.parent;
this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
}
var mychild=new Child("李");
mychild.saySomeThing();
function Parent(firstname)
{
this.fname=firstname;
this.age=;
this.sayAge=function()
{
console.log(this.age);
}
}
function Child(firstname)
{ this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
this.getName=function()
{
return firstname;
} }
var child=new Child("张");
Parent.call(child,child.getName());
child.saySomeThing();
function Parent(firstname)
{
this.fname=firstname;
this.age=;
this.sayAge=function()
{
console.log(this.age);
}
}
function Child(firstname)
{ this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
this.getName=function()
{
return firstname;
} }
var child=new Child("张");
Parent.apply(child,[child.getName()]);
child.saySomeThing();
function Parent()
{ this.sayAge=function()
{
console.log(this.age);
}
}
function Child(firstname)
{
this.fname=firstname;
this.age=;
this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
} Child.prototype=new Parent();
var child=new Child("张");
child.saySomeThing();
function Parent()
{ this.sayAge=function()
{
console.log(this.age);
}
} Parent.prototype.sayParent=function()
{
alert("this is parentmethod!!!");
} function Child(firstname)
{
Parent.call(this);
this.fname=firstname;
this.age=;
this.saySomeThing=function()
{
console.log(this.fname);
this.sayAge();
}
} Child.prototype=new Parent();
var child=new Child("张");
child.saySomeThing();
child.sayParent();
js实现继承的五种方式的更多相关文章
- js实现继承的5种方式 (笔记)
js实现继承的5种方式 以下 均为 ES5 的写法: js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承 ...
- js 实现继承的6种方式(逐渐优化)
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- js 实现继承的几种方式
//js中实现继承的几种方式 //实现继承首先要有一个父类,先创造一个动物的父类 function Animal(name){ this.name = name; this.shoot = funct ...
- 深入浅出js实现继承的7种方式
给大家介绍7中js继承的方法 有些人认为JavaScript并不是真正的面向对象语言,在经典的面向对象语言中,您可能倾向于定义类对象,然后您可以简单地定义哪些类继承哪些类(参考C++ inherita ...
- js实现继承的两种方式
这是面试时面试官会经常问到问题: js的继承方式大致可分为两种:对象冒充和原型方式: 一.先说对象冒充,又可分为3种:临时属性方式.call().apply(): 1.临时属性方式: 当构造对象son ...
- js实现继承的5种方式
js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承有以下通用的几种方式1.使用对象冒充实现继承(该种实现 ...
- JavaScript面向对象(三)——继承与闭包、JS实现继承的三种方式
前 言 JRedu 在之前的两篇博客中,我们详细探讨了JavaScript OOP中的各种知识点(JS OOP基础与JS 中This指向详解 . 成员属性.静态属性.原型属性与JS原型链).今天 ...
- JS实现继承的几种方式
前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一.那么如何在JS中实现继承呢?让我们拭目以待. JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如下: // 定义一个 ...
- js原型继承的几种方式
1. 原型链继承 2,构造函数继承(对象冒充继承) 3,组合继承(原型链继承+构造函数继承) 4,原型式继承 5. 寄生组合式继承 一.原型链继承 function Show(){ this.name ...
随机推荐
- AJAX-跨域解决之 JSONP
(一)AJAX ajax 就是从某个文件中去找相关的数据,把数据拿过来以后,利用数据 分析数据 去做我们想做的事情 分两部分:拿数据 用数据 oUsername ...
- “Invalid maximum heap size” when running Maven
运行mvn package,报错: Invalid maximum heap size: -Xmx512m. Error: Could not create the Java Virtual Mach ...
- windows与linux之间传输文件
1.使用SSH Secure Shell Client 百度网盘 下载地址 http://pan.baidu.com/s/1kTmp00J 2.使用pscp 百度网盘地址:http://pan.bai ...
- 【MongoDB】2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接。
1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errn ...
- 除法 Division
除法 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/A 题意: 输入正整数n按从小到大的顺序输出所有 ...
- [LintCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Oracle 部分函数使用说明
oracle有些函数可能我知道是什么作用,但是具体其实说不清楚,这里是我这几天看到的函数使用方法及说明,记录一下,以后看看 --1.replace('str',oldVal,newVal)替换功能方法 ...
- Android-----工程文件目录介绍
- 使用plupload做一个类似qq邮箱附件上传的效果
公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可 ...
- js 将一段html插入到body最前面
var wrap=document.createElement("div"); var first=document.body.firstChild;//得到页面的第一个元素 va ...