Skip to content

special behaviours of native constructors #28

@michaelficarra

Description

@michaelficarra

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)
    [...]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions