views:

85

answers:

3

Hey, so I'm not entirely sure this is possible, but I am curious if it is possible to hack the definitions of "undefined" objects. Primarily, the purpose of this is to prevent errors from launching when doing a .replace on an object that is undefined. Is it possible to go into the "back end" (is there even such a thing??) and add a method to the undefined object (if that is even defined in the first place??). Essentially something along the likes of

undefined.prototype.replace = function(v,r){ return false };

Obviously this works with String or Object, but undefined, seems to, by nature, have no definition. But it feels like you should still be able to mess with it, since javascript has it as an available end result.

Thanks for your time! I'm really curious as to what will pop up here!

EDIT: The main issue is that I am co-developing with 8 other developers with versioning control, and some of the newer people that come in aren't familiar javascript (or good error handling for that matter), so I wanted to make a global handler for minute errors that can receive default returns if necessary. Doesn't look like this idea will work. Maybe a global try and catch scenario?

A: 

undefined is akin to null; its not an object with a prototype to be overwritten. I don't think you can do this with js code -- you would have to change the way particular js interpreters handle undefined. And doing that is basically changing javascript.

for your specific example of not getting errors when calling a method on undefined; why would you want to do that? Even if possible you are just masking an error in your code and will probably have something worse happen later.

you can always do

var obj = getObj()

if (obj && obj.replace) obj.replace()...

to guard against undefined errors...

Are there any languages that allow you to call a method on an undefined value without an error?

hvgotcodes
That makes a lot of sense. I kind of forgot that the javascript interpreter is dependent on the browser, wah wah, fail. I know that there's a ton of stuff you can do with javascript since it's such a wonky language, but this one was a little far fetched. Masking the error is kind of what I want, since we're dealing with inconsistent webservice returns (I wish I could control that part, but it's outside of our management realm) I wanted to do almost a global try catch scenario on attempts to invoke methods on undefined variables.
Tronhammer
@tronhammer, why not just test the type of of the returned object, and call replace if it exists. edited my answer to demonstrate
hvgotcodes
That is a rule of thumb on my part, but with other developers on the team that aren't as familiar with javascript, I don't have the resources to find all the cases where a sanity check would have been preferred :(
Tronhammer
@tronhammer, then you need some other team members. ;) Or you need to train them up on what is acceptable and what not, and have code reviews.
hvgotcodes
A: 

This may work in some browsers now, but I wouldn't do it. The earlier ECMAScript specifications were a little hazy on the exact nature of undefined, but ECMAScript 5 explicitly describes undefined as read-only. A better approach is to test if the value is undefined:

if (x !== undefined) {
    // OK
} else {
    // do nothing/set default value/skip
}
// alternate approach:
if (typeof x != "undefined") {
    // OK
} else {
    // do nothing/set default value/skip
}

Note that in the first case you must use !== or ===, because some odd cases like undefined == null return true.

C-Mo
See, I wanted to employ this is a prototype of all objects, so that it would prevent an error if the variable in question was indeed undefined.
Tronhammer
Sorry, I wasn't quite clear: it doesn't have to be an error, per se. You could also set a default value or skip it entirely. A hackish way to skip the if/else blocks (if you know the values in question will NEVER correctly evaluate to null/false/0/empty string) is to do something like `x = x || { foo: "bar" }`
C-Mo
+1  A: 

4.3.2 primitive value

member of one of the types Undefined, Null, Boolean, Number, or String as defined in Clause 8.

NOTE A primitive value is a datum that is represented directly at the lowest level of the language implementation.

4.3.10 Undefined type

type whose sole value is the undefined value.

As you can see undefined is a primitive value, and as such you can't define properties to it. Because it's not an object. The best way to think of it is like null. The only difference is that null means intentional emptyness, while undefined is a default value and type for those variables that are yet to be defined.

galambalazs
as for maintaining code quality in your team be sure to check: http://jslint.com/
galambalazs
haha, actually, we looked into this, but can only use MIT licenses. But I tossed that to my boss as a development option for prior to release cleanup
Tronhammer
bureaucracy is a pain in the ass... :)
galambalazs
@Tronhammer you can only *use* tools that have MIT licenses?!? you don't have to deliver or deploy JSLint; it's a script you run against your source. And running it once, prior to release, is not as useful as running it frequently - jslint's purpose is to improve code quality, thereby making further development easier.
DDaviesBrackett