Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript模拟实现Object.create #99

Open
GGXXMM opened this issue May 2, 2021 · 0 comments
Open

JavaScript模拟实现Object.create #99

GGXXMM opened this issue May 2, 2021 · 0 comments
Labels
⭐️ js js knowledge

Comments

@GGXXMM
Copy link
Owner

GGXXMM commented May 2, 2021

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
}
@GGXXMM GGXXMM added the ⭐️ js js knowledge label Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐️ js js knowledge
Projects
None yet
Development

No branches or pull requests

1 participant