Skip to content

JavaScript模拟实现Object.create #99

Open
@GGXXMM

Description

function create(proto, propertyObject=undefined){
  if(typeof proto !== 'object' && typeof proto !== 'function') {
    throw new TypeError('Object prototype may only be an Object or null')
  }
  if(propertyObject === null) {
    throw new TypeError('Cannot convert undefined or null to object')
  }
  function F(){}
  F.prototype = proto
  const obj = new F()
  if(propertyObject != undefined){
    Object.defineProperties(obj, propertyObject)
  }
  if(proto === null){
    obj.__proto__ = null
  }
  return obj
}

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions