Due to function hoisting the functions "createUniqueId" and "fn05"
will be declared before the properties "counter" and "prototype.cntr"
will be assigned. That means that the code will be interpreted as if
it is written like this:

function createUniqueId(){ 
        this.cccc = 1000; 
        alert("
createUniqueId.cntr "+createUniqueId.cntr); 
        return
createUniqueId.counter++;
}

function fn05(){
        var f = new createUniqueId();
        alert(" f.counter "+f.counter);
        alert(" f.cccc "+f.cccc);
        alert(" f.cntr "+f.cntr);
}

createUniqueId.counter = 1;
createUniqueId.prototype.cntr = 99;

Another important thing is that function declarations result in
objects.

createUniqueId instanceof Object ==> true

As a cause of this you can assign properties to the function as you
did.

What you did with the property "counter" was to assign a value only to
the single function-object named "createUniqueId".
By assigning "cntr" to the prototype of the function object
"createUniqueId" every object you create with "new createUniqueId()"
will have the property "cntr" with a value of "99". By using
"this.cccc" also every object you create with "new createUniqueId()"
has also property "cccc" with the value "1000". The difference is that
"cntr" will always be accessible through the prototype chain and
therefore be accessible for objects which inherit with their prototype
from createUniqueId without executing extra code while this.cccc will
be executed in every situation where you create a "new
createUniqueId()" object.
One way to use part of your logic as expected would be to execute
"createUniqueId" without the keyword new, which would always return an
incremented number:


function createUniqueId(){ 
        return createUniqueId.counter++;
}
createUniqueId.counter = 1;

var test = createUniqueId(); ==> test = 2;
var test2 = createUniqueId(); ==> test = 3;

In this case there is a big weakness in the public accessible property
counter which could be changed manually and produce ids which are
already used.

One possible implementation to reach your goal could be the following
one:

var createUniqueId = (function () {
    var id = 0;

    return function() {
        id = id + 1;
        this.id = id;
    }

})();

var unique1 = new createUniqueId();
var unique2 = new createUniqueId();
console.log(unique1.id); // ===> 1
console.log(unique2.id); // ===> 2

This is useful if you just need objects with a unique id which you
then use and work with.

More useful is an implementation where you can pass every object and
the object will receive an id:

var generateUniqueId = (function() {
    var id = 0;

    return function(object) {
        if(typeof object.id != "number") {
            id = id + 1;
            object.id = id;
        }
        return object;
    }
})();

var obj = {};
var anotherObj = {};
generateUniqueId(obj);
generateUniqueId(anotherObj);
console.log(obj.id); // ===> 1
console.log(anotherObj.id); ===> 2


Hopefully this helps you.
If you have any questions so far ask again :-).

Jan

On Aug 23, 7:29 am, Rahul <[email protected]> wrote:
> createUniqueId.counter = 1;
> createUniqueId.prototype.cntr = 99;
>
> function createUniqueId(){
>         this.cccc = 1000;
>         alert(" createUniqueId.cntr "+createUniqueId.cntr);
>         return createUniqueId.counter++;
>
> }
>
> function fn05(){
>         var f = new createUniqueId();
>         alert(" f.counter "+f.counter);
>         alert(" f.cccc "+f.cccc);
>         alert(" f.cntr "+f.cntr);
>
> }
>
> what does my first line of code is,
> whether "counter" is a part of function "createUniqueId" ?
>
> if yes, than why f.counter gives me undifined ?

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to