ES6类与模块
class Animal {
// 构造方法,实例化的时候会被调用,如果不指定,那么会有一个不带参数的默认构造函数
constructor(name, color) {
this.name = name;
this.color = color;
}
// toString 是原型对象上的属性
toString() {
console.log('name: ' + this.name + ',color: ' + this.color);
}
}
var animal = new Animal('dog', 'white');
animal.toString(); // name: dog,color: white
console.log(animal.hasOwnProperty('name')); // true
console.log(animal.hasOwnProperty('toString')); //false
console.log(animal.__proto__.hasOwnProperty('toString')); //true
class Cat extends Animal {
constructor(action) {
// 子类必须要在constructor中指定super方法,否则在新建实例的时候会报错
super('cat', 'white');
this.action = action;
}
toString() {
super.toString();
}
}
var cat = new Cat('catch');
cat.toString();
console.log(cat instanceof Cat); // true
console.log(cat instanceof Animal); // true
类的 prototype 属性和 __proto__ 属性
class Animal {
// 构造方法,实例化的时候会被调用,如果不指定,那么会有一个不带参数的默认构造函数
constructor(name, color) {
this.name = name;
this.color = color;
}
// toString 是原型对象上的属性
toString() {
console.log('name: ' + this.name + ',color: ' + this.color);
}
}
class Cat extends Animal {
constructor(action) {
// 子类必须要在constructor中指定super方法,否则在新建实例的时候会报错
super('cat', 'white');
this.action = action;
}
toString() {
super.toString();
}
}
var cat = new Cat('catch');
console.log(Cat.__proto__ === Animal); // true
console.log(Cat.prototype.__proto__ == Animal.prototype); // true
class Cat {}
console.log(Cat.__proto__ === Function.prototype); // true
console.log(Cat.prototype.__proto__ === Object.prototype); // true
ES6类与模块的更多相关文章
- ES6快速入门(三)类与模块
类与模块 一.类 一)类的声明 class Person { constructor(name) { this.name = name; } sayName() { console.log(this. ...
- ES6中的模块
前面的话 JS用"共享一切"的方法加载代码,这是该语言中最容出错且容易令人感到困惑的地方.其他语言使用诸如包这样的概念来定义代码作用域,但在ES6以前,在应用程序的每一个JS中定义 ...
- ES6 类(Class)基本用法和静态属性+方法详解
原文地址:http://blog.csdn.net/pcaxb/article/details/53759637 ES6 类(Class)基本用法和静态属性+方法详解 JavaScript语言的传统方 ...
- 我的Vue之旅、02 ES6基础、模块、路径、IO
自定义模块 为什么要模块?模块化源代码能给我们带来什么好处? 试想一个巨无霸网购平台,在没有模块化的情况下,如果出现bug,程序员就要在几百万行代码里调试,导致后期维护成本上升,为了解决问题,模块化按 ...
- Python中函数、类、模块和包的调用
初学python阶段,大多数人对函数.类.模块和包的调用都搞得不是很清楚,这篇随笔就简单的进行说明. (1)函数 当函数定义好之后,可以直接调用. 比如:def summ(add1,add2),那么 ...
- javascript基础知识-类和模块
在JavaScript中可以定义对象的类,让每个对象都共享这些属性. 在JavaScript中,类的实现是基于其原型继承机制的.如果两个实例都从同一个原型对象上继承了属性,我们就说它们是同一个类的实例 ...
- [python基础]关于包,类,模块的那些事儿
转载请注明出处:http://www.cnblogs.com/codefish/p/5032753.html 在理解python的包,类,模块之前,我一直是将他类比为dll,C#的类,命名空间的这种参 ...
- React与ES6(三)ES6类和方法绑定
React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...
- 第九章:Javascript类和模块
(过年了,祝大家新年好!) 第6章详细介绍了javascript对象,每个javascript对象都是一个属性集合,相互之间没有任何联系.在javascript中也可以定义对象的类,让每个对象都共享某 ...
随机推荐
- 阅读笔记 The Impact of Imbalanced Training Data for Convolutional Neural Networks [DegreeProject2015] 数据分析型
The Impact of Imbalanced Training Data for Convolutional Neural Networks Paulina Hensman and David M ...
- java 小知识
public static void main(String[] args) { System.out.println( getMonthStart()); System.out.println( g ...
- 单片机TM4C123学习(八):SPI接口D/A
1.头文件和变量定义(不是很清楚) #include "driverlib/ssi.h" #include "driverlib/i2c.h" #include ...
- AjaxControlToolkit MaskedEdit Unspecified error 未指定错误
使用AjaxControlToolkit 里面的 MaskedEditValidator控件,IE里面在如下的js中出现未指定(Unspecified error)错误, if (document.a ...
- [付费视频]Delphi视频Android开发使用静态库(A)和动态库(SO)
关于本视频:前阵子接到一个委托,解决Delphi开发Android程序中串口通信的问题,厂家那边提供了c文件,需要翻译成delphi可用,翻译倒是比较简单.不过后来翻译读写ic卡单元的时候进行不下去了 ...
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
- 【Java学习笔记】<集合框架>TreeSet,Comparable,Comparator
public class Person implements Comparable{ private String name; private int age; public Person(){ su ...
- 使用my exclipse对数据库进行操作(1)
一.查找 public class class1 { public static void main(String[] args) { // TODO Auto-generated method st ...
- Python3学习(2)-中级篇
Python3学习(1)-基础篇 Python3学习(2)-中级篇 Python3学习(3)-高级篇 切片:取数组.元组中的部分元素 L=['Jack','Mick','Leon','Jane','A ...
- 『TCP/IP详解——卷一:协议』读书笔记——05
2013-08-19 22:35:57 2.6 PPP:点对点协议 PPP点对点协议修改了SLIP协议中的所有缺陷: 1. 在串联链路上封装IP数据报的方法.PPP即支持数据为8位&无奇偶检验 ...