Difference between two JS examples?
I've written a JS object that obfuscates email addresses, however I've
discovered two slightly different ways of doing so (each with 1 private
method and 1 public method):
Object 1:
var email = new function()
{
    function encrypt(code, key)
    {
        <eliminated for brevity>
    };
    this.mailto = function(address, name)
    {
        link = encrypt(<code>, <key>);
        document.write('<a href="mailto:'+ link +'">'+ name +'</a>');
    }
};
Object 2:
var email = function()
{
    function encrypt(code, key)
    {
        <eliminated for brevity>
    };
    return {
        mailto: function(address, name)
        {
            link = encrypt(<code>, <key>);
            document.write('<a href="mailto:'+ link +'">'+ name +'</a>');
        }
    };
}();
Both of these syntaxes work and can be called with:
email.mailto('example', 'Example');
I'm particularly interested in memory usage or extensibility.
It looks to me as if Object 1 would create a new instance every time it's
called?
What are the differences?
 
No comments:
Post a Comment