vlastachu
vlastachu

Reputation: 257

define method on built-in type in javascript

Problem for example: In general null object means additional class which handle cases when object not found etc. In Ruby you can also define method for Nil class. And I want realise something similiar in js.

That's what gives me hope typeof(null) //=> object, but in same time null instanceof Object //=> false. So defining method for Object gives me nothing.

So the final question: is there any hacks to define method on built-in types?

Upvotes: 0

Views: 87

Answers (2)

HIRA THAKUR
HIRA THAKUR

Reputation: 17757

You can do that in javascript too.

I am not sure whether I understand your question.But here is my take on it.

alert(typeof(null));//gives you object.

MyObject=Object.create(null);

This means

  1. MyObject doesn't inherit from anywhere

  2. MyObject has no properties at all.

Upvotes: 0

ZenMaster
ZenMaster

Reputation: 12742

Please don't.

That's really bad form and will lead to nasty, hard to find, bugs.

Or if you do - do it with greatest of cares.

Read here for more: http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/

Upvotes: 1

Related Questions