js 面向对象 继承
继承方式有四种:
1、call
2、apply
3、prototype
4、for in
call 和 apply 的主要区别:
call 传参数只能一个一个的传,
apply 因为是用数组,所以可以用arguments 获取所有的实参。当参数多时,就用apply更方便。
arguments = 返回参数集合
call 和 apply 继承
已打飞机游戏为例:
创建飞机时有很多重复的步骤
以创建玩家飞机 用call 继承 和用applay 创建boss飞机为例:
/*父模板*/
/*x,y,节点,blood,speed
* move
* shoot XXX
* init
* */
/*通用的父模板*/
function Plane(x,y){
console.log("plane构造函数")
console.log(this);
this.x = x;
this.y = y;
this.imgNode = document.createElement("img")
this.imgsrc="";
this.blood=5;
this.speed=10;
this.move=function(){
console.log("Plane的move方法");
}
this.init=function(){}
this.init();
}
/*1.call*/
function PlayerPlane(px,py){
console.log(this); //new PlayerPlane()
// call传递参数,参数依次写上
Plane.call(this,px,py); //写在代码第一行
// 重写 ==》多态 同一个方法,不同的实现方式
this.x=1000;
this.move=function(){
console.log("Player Plane 的move方法")
}
this.shoot=function(){}
} var playerplane = new PlayerPlane(200,300);
console.log(playerplane.hasOwnProperty("x")); //true
console.log(playerplane.x)
playerplane.move();
/*2.apply*/
function BossPlane(bx,by){
console.log(arguments); //参数数组
Plane.apply(this,arguments);
this.move=function(){
console.log("Boss飞机的移动");
}
}
var bossplane = new BossPlane(100,200);
console.log(bossplane.x,bossplane.y);
bossplane.move()
原型继承
/*通用的手机模板*/
function Phone(name,price){
this.phoneName = name;
this.price = price;
this.color="red";
this.callPhone=function(){ }
} function IPhone(){
this.color="blue";
this.music=function(){
console.log("听音乐");
}
this.news=function(){
console.log("看新闻");
}
} var iphone1 = new IPhone();
console.log(iphone1.color); //原型链继承
IPhone.prototype = new Phone("苹果",6000);
var iphone2 = new IPhone();
console.log(iphone2.color); console.log(iphone1.__proto__); //原本的Iphone.prototype, 空对象
console.log(iphone2.__proto__); //new Phone()
console.log(iphone1.__proto__===iphone2.__proto__); //false IPhone.prototype.newfunc=function(){
console.log("新添加的功能");
}
iphone1.__proto__.newfunc2=function(){
console.log("另外一个新添加的功能");
}
// iphone1.newfunc2();
// iphone2.newfunc();
console.log("color" in iphone2); //true
console.log(iphone2.hasOwnProperty("color")); //false
Phone.country="中国";
// var p = new Phone();
// console.log(p.country) //undefined 原型链上并没有
// console.log("country" in p) //false
/*p = 实例化对象
* Phone = 函数 --- 函数也是一个对象*/
// console.log(Phone.country); //只有Phone函数才能调用country // var iphone1 = new IPhone();
// console.log(iphone1.country); IPhone.prototype = new Phone();
var iphone2 = new IPhone();
console.log(iphone2.country); //undefined
console.log(iphone2.color); IPhone.prototype = Phone;
var iphone3 = new IPhone();
console.log(iphone3.country); // 打印得出来了
console.log(iphone3.color); // undefined
console.log(iphone3.__proto__.__proto__.__proto__) //prototype 指向一个对象就行了,可以是自己创建的新对象。
IPhone.prototype={
newAttr1:"新属性1",
newAttr2:"新属性2"
}
var iphone4 = new IPhone();
console.log(iphone4.newAttr1);
js 面向对象 继承的更多相关文章
- js面向对象继承
前言 最近看到js面向对象这章节了,主要学习了原型和面向对象继承关系,为了梳理自己的知识逻辑,特此记录. js的面向对象 先说说我目前了解的js创建对象方法 1.写一个函数,然后通过new创建对象 2 ...
- js面向对象 继承
1.类的声明 2.生成实例 3.如何实现继承 4.继承的几种方式 1.类的声明有哪些方式 <script type="text/javascript"> //类的声明 ...
- 关于JS面向对象继承问题
1.原型继承(是JS中很常用的一种继承方式) 子类children想要继承父类father中的所有的属性和方法(私有+公有),只需要让children.prototype=new father;即可. ...
- JS 面向对象 ~ 继承的7种方式
前言: 继承 是 OO 语言中的一个最为人津津乐道的概念.许多 OO 语言都支持两种继承方式:接口继承 和 实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在 ...
- js 面向对象 继承机制
根据w3cschool上的描述:共有3种继承方法(对象冒充,原型链,混合) 1.对象冒充:构造函数ClassA使用this关键字给所有属性和方法赋值,使ClassA构造函数成为ClassB的方法,调用 ...
- JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- JS面向对象(2) -- this的使用,对象之间的赋值,for...in语句,delete使用,成员方法,json对象的使用,prototype的使用,原型继承与原型链
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- js面向对象之继承那点事儿根本就不是事
继承 说道这个继承,了解object-oriented的朋友都知道,大多oo语言都有两种,一种是接口继承(只继承方法签名):一种是实现继承(继承实际的方法) 奈何js中没有签名,因而只有实现继承,而且 ...
- 捋一捋js面向对象的继承问题
说到面向对象这个破玩意,曾经一度我都处于很懵逼的状态,那么面向对象究竟是什么呢?其实说白了,所谓面向对象,就是基于类这个概念,来实现封装.继承和多态的一种编程思想罢了.今天我们就来说一下这其中继承的问 ...
随机推荐
- TSP(个人模版)
O(n^2)TSP: #include<stdio.h> #include<string.h> #include<algorithm> #include<io ...
- BZOJ2441: [中山市选2011]小W的问题
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2441 首先要注意到x1>x3且x5>x3(要是没有这个设定就是树状数组水题了.. ...
- BZOJ 3105: [cqoi2013]新Nim游戏
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3105 题意是要取一些数使得剩余的数xor和的子集不为0 拟阵.求解极大线性无关组.贪心从大到小 ...
- hdu_1027(好吧。。。牛。。。next_permutation也可以水过)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; int ...
- IDEA的破解安装以及汉化
IDEA是一款比eclipse用起来更好用的一款代码编辑器,本人之前也是一直在用eclipse来写代码,后来发现了IDEA用起来会更顺手,所以又转用IDEA了,今天给大家分享一下IDEA的下载安装破解 ...
- c++中的overload、overwrite、override
作为初学者,本文只从语法和简单的使用角度对overload.overwrite.override进行了区分,不曾涉及原理,记录下来以供查阅. 1.verload(重载) 1.1 基本要求: c++中的 ...
- 浅析@Deprecated,调用方法时出现横线划掉样式
Deprecated 这个注释是一个标记注释.所谓标记注释,就是在源程序中加入这个标记后,并不影响程序的编译,但有时编译器会显示一些警告信息. 那么Deprecated注释是什么意思呢?如果你经常使用 ...
- include指令与include动作的区别(面试要考)
include指令: 语法格式:<%@ include file=" " ...%> 发生作用的时间:页面转换期间 包含的内容:页面的实际内容 转换成的servlet: ...
- 如何节省 1TB 图片带宽?解密极致图像压缩
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:Gophery 本文由 腾讯技术工程官方号 发布在云+社区 图像已经发展成人类沟通的视觉语言.无论传统互联网还是移动互联网,图像一直占据着 ...
- linux中的两个命令setfacl和chmod有什么区别
setfacl命令可以用来细分linux下的文件权限.chmod命令可以把文件权限分为u,g,o三个组,而setfacl可以对每一个文件或目录设置更精确的文件权限. 比较常用的用法如下:setfacl ...