0

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?

3
  • 1
    I agree with you, sometimes programmers are too serious (or not enough?). "Don't extend buit-in classes", "Eval is evil"... Annoying quotes, often used as sledgehammer arguments, but also often recited without thinking. That said, I wonder if you could create your own Nil object. Commented Dec 8, 2013 at 10:18
  • @wared Overall, I wanted to hear some old legacy hacks like __proto__ or getters and setters. Now (when I understand futility of my question) typeof(null) //=> object seems like a joke of js developer. It's funny represent it like haskell data object = null | Object a Commented Dec 8, 2013 at 10:40
  • @wared well, I'd say that in this case, and in case of "eval is evil" there is a pretty sound, proven by many years, justification. Commented Dec 8, 2013 at 11:32

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Everything is so serious about the js. I just make little web pages and not impose my approaches.
Well, for us, those who are "so serious" about the JS, it's a matter of professionalism.
0

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.

5 Comments

May be this is right answer. For example var x = myset.findByName("name");/*return null*/ x.getWidth() /*return 0 or other default value*/. To formalise my question: you should execute null.SomeMethod().
But it will be truthy.
Its good that you want to learn about this,but practically this wont be used much..
@Onaseriousnote It can be viewed from two sides. It would be great if you are going to write according to strict standards. At the same time, many people are unaware of the operator delete as not interested in the subtleties of language. Although yes maybe for nothing in language have introduced override built-in objects. And dynamic typing. And a prototype model.
by overriding you mean to manipulate already given functionality...?like for example to modify toString() at its core?

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.