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

special behaviours of native constructors #28

Open
michaelficarra opened this issue Jun 14, 2013 · 0 comments
Open

special behaviours of native constructors #28

michaelficarra opened this issue Jun 14, 2013 · 0 comments

Comments

@michaelficarra
Copy link

Certain specified behaviours of native constructors can't be simulated, such as setting the [[Class]] and [[PrimitiveValue]] properties. This prevents us from constructing an instance of these constructors with a nondeterministic number of arguments (as you would with apply normally). Here's how we can try to instantiate Date the way we would any other constructor with a dynamic set of arguments:

+function(ctor, args){
  var tmpCtor = function(){};
  tmpCtor.prototype = ctor.prototype;
  var obj = new tmpCtor;
  var result = ctor.apply(obj, args)
  return Object(result) === result ? result : obj;
}(Date, [1990, 01, 01]);

You would expect this to produce 633852000000, but because it doesn't have a [[PrimitiveValue]], it throws the error

TypeError: this is not a Date object.
    at Date.valueOf (native)
    [...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant