Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-08-2012, 07:00 AM   PM User | #1
salmanmanekia
New Coder

 
Join Date: Nov 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
salmanmanekia is an unknown quantity at this point
undefined expected but getting false

Hi All,

I am new to JS and the web dev in general. I am coming from background of C++ and Java. Below is a piece of code which confuses me and i want your opinions into why it works as its working now

Code:
var aVar = {
        not:function()
                {return !this.tVar},
        tVar:true
}

function chkChk() {
       document.write(aVar.not()); //false
       document.write(aVar.b);  // undefined
}

Now, in the above code i have been told that aVar.not() should result in undefined and my logic also says soo because the function not returns tVar which is initialized to true after the not function.
But, the problem is it returns false. Why ??
salmanmanekia is offline   Reply With Quote
Old 11-08-2012, 08:51 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
One thing you seem to misunderstand here:

tVar: true is not really an assignment operation, it is rather a definition of a key/value pair inside an object literal. Key/Value pairs in object literals do not have an order! So once you defined the object literal like that, all of its key/value pairs are defined and if you access tVar from another key ("not()" in your case), tVar will be true.
devnull69 is offline   Reply With Quote
Old 11-08-2012, 09:05 AM   PM User | #3
salmanmanekia
New Coder

 
Join Date: Nov 2012
Posts: 10
Thanks: 3
Thanked 0 Times in 0 Posts
salmanmanekia is an unknown quantity at this point
Hmm, So i guess i have been told incorrectly that aVar.not() results in undefined. Can you also please tell me what does 'this' refers to in the above context. Does it refer to aVar ??
salmanmanekia is offline   Reply With Quote
Old 11-08-2012, 09:27 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Yes, it refers to the current object which is aVar.
devnull69 is offline   Reply With Quote
Old 11-08-2012, 08:09 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
It's really not different than, say, a C++ class:
Code:
class aClass {
  public:
    bool not() { return ! this.tVar; }
  private:
    bool tVar = true;
}

aClass aVar = new aClass( );
except that the class declaration *AND* the instantiation of an instance thereof are combined. Hence the name "object literal". See the parallel to "string literal", an example of which would be just "this is a string literal".

In JavaScript, since there are no classes, we could create an [b]aVar[b] object this:
Code:
// constructor for an oVar object:
function oVar( )
{
    this.tVar = true;

    function not( ) { return ! this.tVar; }
}

var aVar = new oVar( );
And that works just fine. But it's essentially identical to the object literal
Code:
var aVar = {
        not : function() {return !this.tVar},
        tVar : true
};
By the by, you could also write that as:
Code:
var aVar = {
        "not" : function() {return !this.tVar},
        "tVar" : true
};
and you could invoke either
Code:
    aVar.not( );
or
    aVar["not"]( );
or
    var foo = "no";
    aVar[foo + "t"]( );
100% equivalently. Do you begin to see some possibilities?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
salmanmanekia (11-10-2012)
Old 11-08-2012, 08:11 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Forgot to say: One good reason to use a constructor instead of an object literal is obvious by example:

Code:
function oVar( init )
{
    this.tVar = init;

    function not( ) { return ! this.tVar; }
}
With a constructor, you are getting much closer to C++/Java style constructors, wherein you can pass in values for initialization.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 06:17 AM   PM User | #7
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
On the other hand side, object literals are Singletons (should you need one).
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 11-09-2012, 07:11 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, sure.

But I think that comes naturally.

In JavaScript, as in most modern languages (including C++, Java, C#, etc.), variables contain *references* to objects, not the actual objects [okay, I know there is a way to make an exception to this in C++].

So if you do
Code:
var aVar = { xxx : yyy, zzz: aaa };
then of course is you later do bVar = aVar; you are only copying the *reference*. The object remains unchanged. There's no way to have another one of this same object.

But, yes, it's an interesting design pattern. Not sure how useful it is to think of it as a singleton, but it is.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 07:22 AM   PM User | #9
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by Old Pedant View Post
Not sure how useful it is to think of it as a singleton, but it is.
It fulfills the objective of a Singleton as to "only allow one single instance". That it looks different from what we’re used to in class-based languages doesn’t matter. Another way for a Singleton is the Module pattern, which, incidentally (though with a slightly different intend), also uses an object literal for that purpose.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 11-09-2012, 07:27 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Sorry...I just meant that you don't need to know the term "singleton" to realize that each object literal must be uniquely itself. So a JS programmer wouldn't need to think of it as "singleton" if the term doesn't mean much to him/her.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 09:19 PM   PM User | #11
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
Sorry...I just meant that you don't need to know the term "singleton" to realize that each object literal must be uniquely itself. So a JS programmer wouldn't need to think of it as "singleton" if the term doesn't mean much to him/her.
By the definition of what a singleton is that is used in object oriented languages, all objects in JavaScript are singletons because there are no classes.

You can copy objects in JavaScript - just not using a simple assignment.

Code:
deepCopy = function(p,c) {
var c = c||{};
for (var i in p) {
  if ('object' === typeof p[i]) {
    c[i] = (p[i].constructor === Array)?[]:{};
    deepCopy(p[i],c[i]);
  } else c[i] = p[i];}
return c;
}
 
bVar = deepCopy(aVar);
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-09-2012, 09:32 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Agreed. Technically, all JS objects are singletons. But it's not very useful to think of them that way if, for example, they were created via a constructor.

And in most class-oriented languages, a singleton pattern means one in which you can NOT create more than one object of the given class. By that pattern (not by that definition), no constructor-based object in JS is a singleton.

So it's really a matter of how you want to view things and whether it really matters if you relate JS to other languages.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 09:39 PM   PM User | #13
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by felgall View Post
By the definition of what a singleton is that is used in object oriented languages, all objects in JavaScript are singletons because there are no classes.
I’m with Old Pedant here. I no definition of Singleton that I know is the term "classes" included. I always read about instances of objects.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 11-09-2012, 11:18 PM   PM User | #14
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
Agreed. Technically, all JS objects are singletons. But it's not very useful to think of them that way
I agree - that's why my JavaScript course includes the following code as a way to create in JavaScript what most people actually intend when they refer to a singleton:

Code:
single = function(x) {
  single.only = function() {
    if (typeof one_only === 'undefined') one_only = this;
    };
  single.only();
  one_only.a = x;
  return one_only;
}
 
t1 = new single(1);
t2 = new single(2);
which results in t1 and t2 both pointing to the same 'singleton' object.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-09-2012, 11:24 PM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Cute. But it does require one_only as page scope variable, so if you have a few different singletons on the page presumably you'd want a longer name with each and one not likely to collide. I do like the concept!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:58 AM.


Advertisement
Log in to turn off these ads.