0

the question is straight forward. Just like in PHP we have the magic function __construct(), is there any related function or hack I can use in javascript?

Example:

function setLength() {
    /* Some work */
}

var a = new Object();
b = new String("Hello");
//Is there anyway the function setLength() will automatically be fired when an Object or String... created?

I'm looking forward to your answers. Thank a lot for any help.

[x]

xx3004
  • 760
  • 2
  • 11
  • 20

3 Answers3

2

Trying to overload String would be a bad idea, especially if working with third party libraries. Same would go for augmenting Object.

However, here is how you may do it, but I don't recommend it..

var _String = String;

window.String = function() {
        setLength();
        return new _String(arguments[0]);
}

This obviously won't be called when creating a primitive string too.

jsFiddle.


You could use a string factory function that returns a new String object and calls your function.

var stringFactory = function(chars) {
    setLength();
    return new String(chars);  
}

This has some advantages, mainly the String constructor is not overloaded.

alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    +1, for the stringfactory. However, I don't think the first part is really a viable approach. but, even so I would suggest using a closure to avoid creating that `_String` global variable, like `window.String = (function(){var _String = String;setLength();return new _String(arguments[0]); })();` – smartcaveman Dec 12 '11 at 08:57
  • Thank you Alex, this until now have been the most perfect solution. I've tried to avoid it if I can. So basically when we copy/clone the String constructor and assign it to another variable then reuse it, it makes no bad effect right? [x] – xx3004 Dec 13 '11 at 00:32
1

You can just create your own object by doing something like this. The function acts as the constructor:

$(document).ready(function(){

    var instance = new object('test123');

    alert('Instance: '+instance.len);

    function object(var1){
        this.var1 = var1;

        this.len = this.var1.length;
    }
});
Stijn_d
  • 1,078
  • 9
  • 20
  • Hi. It is a good way, I've thought of it, but I'd like to add the functions to the native Object instead of create a new one. [x] – xx3004 Dec 13 '11 at 00:34
0

You can override string.prototype.constructor to alter the default functionality of the string constructor, and object.prototype.constructor to alter the default functionality of the object constructor.

However, overriding the default functionality of JavaScript's core classes is widely considered to be bad practice, and there is likely a better way to accomplish your end goal. If you do decide to do this, you might also benefit from reading the following SO Post about overriding a function with a function that references the overridden function: Overriding a JavaScript function while referencing the original

After messing around with jsFiddle, and then doing some research. I found out that I'm actually wrong. String primitives are immutable, so you can't override the constructor, as I suggested. Check out String prototype modifying itself, for more info on that.

Community
  • 1
  • 1
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • I am thinking that I would not replace the whole String construction, but instead inject some code into it, such as to know which function(s) is activated when a String is created, then we can only override that function. Is it possible? Do developers have any hack around this problem? I am just curious. [x] – xx3004 Dec 12 '11 at 08:24
  • How would you achieve this by modifying the `constructor` property? – alex Dec 12 '11 at 08:26
  • @xx3004, It doesn't seem like there is going to be a way to do what you are describing. Your best bet is to use a consistent `stringFactory` pattern to create strings like in the end of @alex's example – smartcaveman Dec 12 '11 at 08:54