Giter Site home page Giter Site logo

lianxi5's Introduction

继承

ES6

开始有 class 语法,可以直接定义和 extends 一个类型:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes

ES5

以前没有类型,所以大家模拟了很多继承的方法,但本质上都是两种的变体:

  • 原型链继承
// 假设有一个需要继承的一个类型 Animal

function Cat() {}
Cat.prototype = new Animal
// 添加一个属性
Cat.prototype.name = 'cat'
  • 构造继承
// 假设有一个需要继承的一个类型 Animal

function Cat(name){
  Animal.call(this)
  // 添加一个属性
  this.name = name || 'cat'
}
  • 所以组合这两个东东:组合继承
// 假设有一个需要继承的一个类型 Animal

function Cat(name) {
  Animal.call(this)
  // 添加属性
  this.name = name || 'cat'
}
Cat.prototype = new Animal()
// 添加方法
Cat.prototype.say = function () {
  // TOOD
}
  • 优化内存节省一些:
// 假设有一个需要继承的一个类型 Animal

function Cat(){
  Animal.call(this)
  this.name = 'cat'
}

(function(){
  // 创建一个没有实例方法的类
  var Super = function () {}
  Super.prototype = Animal.prototype
  // 将实例作为子类的原型
  Cat.prototype = new Super()
  // 添加方法
  Cat.prototype.say = function () {
    // TOOD
  }
})()

lianxi5's People

Contributors

loskael avatar

Stargazers

 avatar

Watchers

 avatar  avatar

lianxi5's Issues

对 README 的第一条小建议。

原型继承:

function Animal() {}
function Cat() {}
Cat.prototype = new Animal;

在执行完上述语句后,Cat.prototype.constructor 由 Cat 变成了 Animal, 所以我觉得加上一个语句让其构造函数指回 Cat 会更好。如下:

function Animal() {}
function Cat() {}
Cat.prototype = new Animal;
// 新增一句
Cat.prototype.constructor = Cat;

Something QUIRKY About 'instanceof' and '__proto__'

我在浏览器里做了如下实验

var a = new String('123')
// undefined
a instanceof String
// true
a instanceof Object
// true
a.__proto__ === String.prototype
// true
a.__proto__.__proto__ === Object.prototype
// true

但是当我使用字面量定义的时候

var b = '123'
b instanceof String
// false
b instanceof Object
// false
b.__proto__ === String.prototype
// true
b.__proto__.__proto__ === Object.prototype
// true

所以字面量赋值这是什么魔法。。。可以保留__proto__的指向的同时骗过instanceof的判断?

instanceof on MDN

如何调试测试

老师 可以演示一下测试的调试过程吗,mocha和karma怎么调试,打断点

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.