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面向对象的继承问题
说到面向对象这个破玩意,曾经一度我都处于很懵逼的状态,那么面向对象究竟是什么呢?其实说白了,所谓面向对象,就是基于类这个概念,来实现封装.继承和多态的一种编程思想罢了.今天我们就来说一下这其中继承的问 ...
随机推荐
- 【Java学习笔记之二】java标识符命名规范
什么是标识符 就是程序员在定义java程序时,自定义的一些名字.标识符可以应用在类名.变量.函数名.包名上. 标识符必须遵循以下规则 标识符由26个英文字符大小写(a~zA~Z).数字(0~9).下划 ...
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...
- linux系统下,警告:warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration] 和 warning: the `gets' function is dangerous and should not be used. 的由来和解决方法。
字符数组 的英文名字是 char [] gets()函数的基本用法为:char *gets(char *s); 该函数的参数是一个字符数组,该函数的返回值也是一个字符数组. linux下的代码如下: ...
- jquery 和 mui 上拉加载
jquery: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <m ...
- 解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
重新启动服务器,访问web服务发现无法浏览啦!登陆服务器之后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr ...
- 003_JS基础_面向对象基础
3.1 对象 引入:在js中表示一个人的信息(name, gender, age)通过var申明三个变量,但是这样使用基本数据类型的变量,他们是互相独立的,没有联系: 此时就需要使用对象,对象是 ...
- angular4 中自定义pagination组件
你用Angular 吗? 一.介绍 一个基于angular4 开发的可以分页的组件.组件的好处就是可以复用,复用.....作为一个前端码农,开始的分页功能实现是我用jquery写的,其他同事用的时候都 ...
- 使用 IDEA和Maven 整合SSH框架
1.创建web工程 一路next 下去就行.完成后,IDEA会自动构建maven工程. 2.创建如下项目结构 需要将 java文件夹设置为SourcesRoot目录,否则无法创建package 设置操 ...
- Django之modelform组件
一.简介与基本使用 简介:django中的modelform组件同时具有model和form作用,但是耦合度比较高,当项目需要拆分时候就比较困难了,所以在使用modelform时候需要先考虑项目的扩展 ...
- 深入理解HashMap的扩容机制
什么时候扩容: 网上总结的会有很多,但大多都总结的不够完整或者不够准确.大多数可能值说了满足我下面条件一的情况. 扩容必须满足两个条件: 1. 存放新值的时候当前已有元素的个数必须大于等于阈值 2. ...