Newbie question about variables...

B

Blue Streak

Alright,

What am I doing when I define a varialble as such:

var myVariable = { ... }

.... and what is this called?
 
B

Blue Streak

Hi,

That way you create an Object in Javascript. Have a look athttp://json.orgto get more info.

bye








- Show quoted text -

Okay, just to shoot a dead horse:
Would something like this becalled a JSON Class Prototype?

var MyVariable = {
a : '',
b : 5,
c : null,
doThis : function() {
// ...
},
doThat : function() {
// ...
}
}
 
B

Benjamin Sattler

Hi!

I'm not that sure about the exact terminology here, but according
to that website I mentioned I would rather just call it "object"
oder maybe "object literal":

An object is an unordered set of name/value pairs. An object begins
with { (left brace) and ends with } (right brace). Each name is
followed by : (colon) and the name/value pairs are separated by ,
(comma).

Prototypes IMHO are the basic objects in js from which all objects
you create are being cloned. Look at that code here

[snip]
//Create a string
var val1 = "Hello World!";
//does not exist yet => error
alert(val1.return1st());

//now we modify the String-prototype
//and add our method
String.prototype.return1st = function()
{
return this.substr(0,1);
}
//does work now, because we modified the prototype
alert(val1.return1st());

//Create another String
var val2 = "Yet another String";
alert(val2.return1st());
[/snip]

the last line of code will return a "Y" as expected because the
referenced method has been added to the String-prototype (and not
only to the String object of val1). Every String that you create
will be a copy of the prototype and therefore have our newly
implemented return1st()-method.

bye
 
R

RobG


Google Groups seems to be having some serious problems, so I
appologise in advance if I am repeating what others have said. I
can't see any new messages after about 2007/07/17 20:30 GMT.

Please don't top-post, reply below trimmed quotes.

I'm not that sure about the exact terminology here, but according
to that website I mentioned I would rather just call it "object"
oder maybe "object literal": [...]
Prototypes IMHO are the basic objects in js from which all objects
you create are being cloned.

Don't think that. If objects are "clones" of anything, it is the
built-in javascript Object object. Prototype inheritance only works
for functions, not for plain objects.

But don't think of clones at all, think of objects constructed by
constructor functions (like Object, String, etc.).

Look at that code here
Let's.



[snip]
//Create a string
var val1 = "Hello World!";
//does not exist yet => error

The variable val1 "exists" when variables are instantiated, it is
assigned a value a little later when the code is executed.

alert(val1.return1st());

Here javascript treats vall (a string primitive) as a String object,
but only for the purposes of evaluating the statement
val1.return1st(). val1 stays as a string primitive.
//now we modify the String-prototype
//and add our method
String.prototype.return1st = function()

String has a prototype because it is a function object. A return1st
property is added during instantiation which is before your call to it
above is executed. But, since you are assigning a value using a
function expression, it doesn't get added until the code is executed,
which is after you've tried to call it. So it fails because it's a
straight syntax error.

{
return this.substr(0,1);}

//does work now, because we modified the prototype
alert(val1.return1st());

Which shows that String objects "inherit" the properties of
String.prototype. That occurs because val1's scope chain (when val1
is treated as an object) includes its constructor's prototype (i.e.
String.prototype).

It does not make a string primitive a "clone" of the prototype - don't
think of clones at all.

//Create another String
var val2 = "Yet another String";
alert(val2.return1st());
[/snip]

the last line of code will return a "Y" as expected because the
referenced method has been added to the String-prototype

Why not write "String.prototype"?
(and not
only to the String object of val1).

val1 isn't a String object, it's a primitive.
Every String that you create
will be a copy of the prototype and therefore have our newly
implemented return1st()-method.

No, every string object inherits return1st() because it has
String.prototype on its scope chain (unless that chain is otherwise
modified).

You might like to read Richard Cornford's article on Closures, it has
some good stuff on how javascript works even if you aren't interested
in closures themselves:

<URL: http://www.jibbering.com/faq/faq_notes/closures.html#clIRExSc >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top