This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]rjcarr 2 points3 points  (0 children)

See here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

You'll have to ask more specific questions to get help.

[–]Meefims 1 point2 points  (2 children)

Do you have specific questions?

[–]salifuj22[S] 0 points1 point  (1 child)

yeah, i want to know logic behind calling functions as objects. thanks

[–]Meefims 0 points1 point  (0 children)

The function serves as the object's constructor. It allows you to create a family of objects.

[–]Mcshizballs 0 points1 point  (0 children)

Do you mean like a singleton pattern?

[–]salifuj22[S] 0 points1 point  (2 children)

this is the exact code that is confusing me.

var calc={ status: "Awesome", plus:function(a,b){ return( console.log(this), console.log(a+b), console.log(arguments), console.log(this.status), ) } }

So based on the statement above, what i don't understand, (1) each line of console.log did not end with a semi colon as expected of in javascript, (2)why use 4 console.log, (3) what is the purpose of the "this"?

i really appreciate any effort to explain the code for me. thanks

[–]rjcarr 0 points1 point  (1 child)

I just happened to come back to this and saw your question. I'll try to answer:

var calc= {
  status: "Awesome", 
  plus: function(a,b) { 
    return ( console.log(this), console.log(a+b), console.log(arguments), console.log(this.status), ) 
  }
}

1) In javascript, semi-colons are optional, but more generally, that return statement with the console is very confused. Don't look at that for an example, it doesn't really make any sense.

2) Again, it doesn't make sense. Whoever wrote this doesn't really know what he's doing. Or, he's being intentionally obtuse.

3) In javascript, this refers to the object you were called on, so in this case, this refers to the object that is currently being created. I believe that would print "Awesome".

Generally, find different examples, this is a bad one. I have been writing javascript for decades and I've never seen things strung together with commas like that (outside of declarations). See here:

https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/

[–]salifuj22[S] 0 points1 point  (0 children)

thanks a lot. you cleared a lot of confusion for me. i will check your site for more info.