ES5和ES6创建类
function Point(x,y){
this.x = x;
this.y = y;
}
Point.prototype.getPostion = function(){
return '(' + this.x + ',' + this.y')'
}
var p1 = new Point(2,3);
console.log(p1)
console.log(p1.getPostion())
class Point{
constructor(x,y){
this.x = x;
this.y = y;
}
getPostion () {
return `(${this.x},${this.y})`
}
}
var p1 = new Point(2,3);
console.log(p1)
console.log(p1.getPostion())
console.log(p1.hasOwnProperty('x'))
console.log(p1.hasOwnProperty('getPostion'))
console.log(p1.__proto__.hasOwnProperty('getPostion'))
取值函数 存值函数
var info = {
_age:18,
set age(newValue){
if (newValue > 18){
console.log('哭~~~')
} else {
console.log('笑~~~')
}
},
get age(){
console.log('秘密~~~~')
return this._age
}
}
console.log(info.age)
info.age = 17
class Info(){
constructor(age){
this._age = age
}
set age (newAge){
console.log('new age is:' + newAge)
this._age = newAge
}
get age(){
return this._age
}
}
const infos = new Info(18)
infos.age = 17
console.log(infos.age)
class表达式
const Func = function(){}
function Func(){}
const Infos = class{}
class Infos = {}
静态方法
class Point {
z = 0;
constructor(x,y){
this.x = x;
this.y =y;
}
static getClassName(){
return Point.name
}
}
Point.getClassName()
const p = new Point()
私有变量
//第一种
class Point{
_name(){
//私有方法 只是约定 还是可以被外部调用
}
}
//第二种
const _fn = () => {}
class Point{
fn1(){
_fn.call(this)
}
}
// export default Point 只暴露这个类外部就没办法访问到_fn
//第三种
//a.js
const fn = Symbol('fn');
export default class Point{
}
//b.js
import Point from './a.js'
//只可以访问point
//第四种
class Point {
#oenProp = 12
}
new target
function Point(){
console.log(new.target)
}
const p = new Point();
function Point(){
constructor(){
console.log(new.target)
}
}
const p = new Point();
function Parent(){
constructor(){
console.log(new.target)
}
}
class Child extends Parent{
constructor(){
super()
}
}
继承
function Food(){
this.type = 'food'
}
Food.prototype.getType = function(){
return this.type
}
function Vegetables(name){
this.name = name
}
Vegetables.prototype = new Food();
const tomato = new Vegetables('tomato')
console.log(tomato.getType())
class Parent{
constructor(name){
this.name = name
}
getName(){
return this.name
}
}
class Child{
constructor(name,age){
super(name)
this.age = age
}
}
const c = new Child('kimi',18)
console.log(c)
console.log(c.getName())
console.log(c instanceof Child)
console.log(c instanceof Farent)
console.log(Object.getPrototypeOf(Child) === Parent)
super
class Parent {
constructor(name){
this.name = name
}
getName(){
return this.name
}
static getNames (){
return this.name
}
}
class Child extends Parent{
constructor(name,age){
super(name)
this.age = age
}
}
const c = new Child('kimi',18)
console.log(c.name)
console.log(Child.getNames())
class Parent{
constructor(){
this.type = 'parent'
}
getName(){
return this.type
}
}
Parent.getType = () => {
return 'is parent'
}
class Child extends Parent{
constructor(){
super()
console.log('constructor:' + super.getName())
}
getParentName(){
console.log(super.getName)
}
getParentType(){
console.log(super.getType())
}
static getParentType(){
console.log(super.getType())
}
}
const c = new Child()
c.getParentName()
c.getParentType()
Child.getParentType()
class Parent{
constructor(){
this.name = 'parent'
}
print(){
console.log(this.name)
}
}
class Child extends Parent{
constructor(){
super()
this.name = 'child'
}
childPrint(){
super.print()
}
}
const c = new Child()
c.childPrint()
proto
var objs = new Object()
console.log(objs.__proto__ === Object.prototype)
原生实例的继承
class CustomArray extends Array{
constructor(...args){
super(...args)
}
}
const arr = new CustomArray(3,4,5)
console.log(arr.fill(0))
console.log(arr)
为什么super 必须在this之前
- 普通的构造函数执行时,会创建一个空对象作为this
- 继承构造函数,需要父类完成这件事情,只有调用super 才会创建空对象 才会又this 负责就会报错