1

I am curious why this returns the same timestamp for each. The object should have different identifiers I would think.?

/js/helpers/v01.js

var Tester = (function () {
    var object_id = 'Tester';
    var object_id_unique = (new Date().getTime()) + '-' + (new Date().getMilliseconds());
    var _this;

    /**
     *
     * @constructor
     */
    function Tester(obj_name) {
        this.name = obj_name;
        this.run();
    }

    Tester.prototype = {

        run: function () {
            "use strict";
            var $body = document.getElementsByTagName('body')[0];
            var $node = document.createElement('div');
            $node.innerHTML = '<lable>' + this.name + ': </lable>' + ' ' + object_id + '-' + object_id_unique;
            $body.appendChild($node);
        }
    };
    return Tester;
})();

Here is the page

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="/js/helpers/v01.js"></script>
</head>
<body>

<script type="text/javascript">
    new Tester('A');
    setTimeout(function () {
        new Tester('B');
    }, 500);
</script>
</body>
</html>

My output return this

A: Tester-1385613846838-838
B: Tester-1385613846838-838
1
  • Put console.log('foo') before var object_id_unique = ...; and see how often it executes. I recommend to learn how to debug JavaScript so that you can set breakpoints, inspect variables etc. Commented Nov 28, 2013 at 5:12

2 Answers 2

1

In your closure you're permanently setting the value of object_id_unique (as the function is called immediately upon definition, not when you call the returned function) - move that inside the returned function. This should fix it:

var Tester = (function () {
    var object_id = 'Tester';
    var _this;

    /**
     *
     * @constructor
     */
    function Tester(obj_name) {
        this.name = obj_name;
        this.run();
    }

    Tester.prototype = {

        run: function () {
            "use strict";
            var object_id_unique = (new Date().getTime()) + '-' + (new Date().getMilliseconds());
            var $body = document.getElementsByTagName('body')[0];
            var $node = document.createElement('div');
            $node.innerHTML = '<lable>' + this.name + ': </lable>' + ' ' + object_id + '-' + object_id_unique;
            $body.appendChild($node);
        }
    };
    return Tester;
})();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, makes sense. I had multiple objects like this one and was scratching my head why there the same... got it.
1

You're only creating object_id_unique once, when you define the class. If you want it to be different in each instance, you need to assign it in the constructor.

Comments

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.