0

Is there a node.js way to extend native JS objects, similar to clientside:

Date.prototype.tomorrow = function(){
  return this.getTime()+86400000;
}

such that

var dt = new Date();
dt.tomorrow();

works as expected.

2 Answers 2

3

This works exactly like in the browser, since Node is built on top of V8 (Google Chrome's engine). Save the following code and run it with Node:

Date.prototype.tomorrow = function(){
  return this.getTime()+86400000;
}
var dt = new Date();
console.log(dt.tomorrow());
Sign up to request clarification or add additional context in comments.

Comments

1

That ought to work. Prototypes are a plain old JavaScript feature, not Node-specific. In fact, this is what the Sugar library does, but you should read their notes on this.

1 Comment

thanks for the ref to Sugar. Any advice on how to do this as a module so that it can be pulled in easily into projects?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.