You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function_defineProperties(target,props){for(vari=0;i<props.length;i++){varprop=props[i]// 如果存在value,即表示为可写入的if('value'inprop)prop.writable=true// configurableObject.defineProperty(target,prop.key,prop)}}function_createClass(Constructor,protoProps,staticProps){// 如果是原型方法,其应该是挂载在原型上if(protoProps)_defineProperties(Constructor.prototype,protoProps)// 如果是静态方法,则是直接挂载在构造函数上,静态方法不需要实例化该函数即可使用if(staticProps)_defineProperties(Constructor,staticProps)returnConstructor}varParent=function(){'use strict';// 类声明和类表达式的主体都执行在严格模式下'functionParent(options){// 判断当前this是否属于构造函数Parent的一个实例_classCallCheck(this,Parent)this.name=options.name||''this.age=options.age||20}_createClass(Parent,[{key: 'find',value: function(v){return''.concat(v,'慢慢慢慢慢慢')}},{key: 'info',get: function(){return'name is '.concat(this.name).concat(',age is ',this.age)}}],[{key: 'eat',value: function(){return''.concat(this.name,' eat apple')}}])returnParent}()constparent=newParent({name: '流了颗星'})console.log(parent.find('666'))// 666慢慢慢慢慢慢console.log(parent.info)// name is 流了颗星,age is 20console.log(parent.eat())// Parent eat apple
子类实现继承
function_inherits(subClass,superClass){// 判断父类是否是符合要求的if(typeofsuperClass!=='function'&&superClass!==null){thrownewTypeError('Super expression must either be null or a function')}// 让子类继承父类的公有方法// Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__// 大致为 // subClass.prototype = Object.create(superClass.prototype)// subClass.prototype。constructor = subClasssubClass.prototype=Object.create(superClass&&superClass.prototype,{constructor: {value: subClass,writable: true,configurable: true},})// 让子类继承父类的静态方法,即subClass.__proto__ = superClassif(superClass)Object.setPrototypeOf(subClass,superClass)}constChild=function(Parent){_inherits(Child,Parent)// 继承父类方法functionChild(options){_classCallCheck(this,Child)constval=Parent.call(this,options)// 如果父级返回了一个对象类型,则把this指向为这个返回的对象if(typeofval==='object')returnvalreturnthis}returnChild}(Parent)
完整实现
function_classCallCheck(instance,Constructor){// 检测instance 是否是 Constructor 的一个实例if(!(instanceinstanceofConstructor)){// 如果不是,则抛出一个类型错误thrownewTypeError('不能直接作为函数调用')}}function_defineProperties(target,props){for(vari=0;i<props.length;i++){varprop=props[i]// 如果存在value,即表示为可写入的if('value'inprop)prop.writable=true// configurableObject.defineProperty(target,prop.key,prop)}}function_createClass(Constructor,protoProps,staticProps){// 如果是原型方法,其应该是挂载在原型上if(protoProps)_defineProperties(Constructor.prototype,protoProps)// 如果是静态方法,则是直接挂载在构造函数上,静态方法不需要实例化该函数即可使用if(staticProps)_defineProperties(Constructor,staticProps)returnConstructor}function_inherits(subClass,superClass){// 判断父类是否是符合要求的if(typeofsuperClass!=='function'&&superClass!==null){thrownewTypeError('Super expression must either be null or a function')}// 让子类继承父类的公有方法// Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__// 大致为 // subClass.prototype = Object.create(superClass.prototype)// subClass.prototype。constructor = subClasssubClass.prototype=Object.create(superClass&&superClass.prototype,{constructor: {value: subClass,writable: true,configurable: true},})// 让子类继承父类的静态方法,即subClass.__proto__ = superClassif(superClass)Object.setPrototypeOf(subClass,superClass)}varParent=(function(){'use strict'// 类声明和类表达式的主体都执行在严格模式下'functionParent(options){// 判断当前this是否属于构造函数Parent的一个实例_classCallCheck(this,Parent)this.name=options.name||''this.age=options.age||20return'53443'}_createClass(Parent,[{key: 'find',value: function(v){return''.concat(v,'慢慢慢慢慢慢')},},{key: 'info',get: function(){return'name is '.concat(this.name).concat(',age is ',this.age)},},],[{key: 'eat',value: function(){return''.concat(this.name,' eat apple')},},])returnParent})()varChild=function(Parent){_inherits(Child,Parent)// 继承父类方法functionChild(options){_classCallCheck(this,Child)constval=Parent.call(this,options)// 如果父级返回了一个对象类型,则把this指向为这个返回的对象if(typeofval==='object')returnvalreturnthis}returnChild}(Parent)constparent=newParent({name: '流了颗星'})console.log(parent)console.log(parent.find('666'))// 666慢慢慢慢慢慢console.log(parent.info)// name is 流了颗星,age is 20console.log(Parent.eat())// Parent eat appleconstc=newChild({name: '六六六'})console.log('c----',c.info)// name is 六六六,age is 20
The text was updated successfully, but these errors were encountered:
只能作为构造函数使用,作为普通函数时会报错
给父类添加方法
子类实现继承
完整实现
The text was updated successfully, but these errors were encountered: