Jump to content

Object instance


tadpole

Recommended Posts

style of an Object instance ?the W3C tutorial shows a instance of a object like this:personObj=new Object();personObj.firstname="John";personObject.lastname="Doe";and the instruction that i am looking at else where shows placing the values inside of the parentheses, as well as assigning the instance to a varable, while yllow_cat is an existing object, example:var fat_cat = new yllow_cat("hunt", "fish", "sleep");could someone please give some thought in text on the subject at hand, as to what is legal or more marginally correct towards ECMA script.or is this a style thing.thank you

Link to comment
Share on other sites

The second would only work if you had a function called yllow_cat that received "hunt", "fish", "sleep" as arguments and assigned them to members of an object. Like so:

function yllow_cat (a, b, c) {   this.sport = a;   this.food = b;   this.hobby = c;}

Both are legal. A third way is to create an "object literal":

p={firstname: "john", lastname: "Doe"};

A lot of this is preference. Note that the first and third techniques can not be re-used as written, where the second can create an arbitrary number of objects.

Link to comment
Share on other sites

Thank you, Deirdre's Dadyes i do see that this instance as i showed was formed after a constructor function.And the Object Literal was another question i had, while the word literal is used.so each to its own instance.a function or a literal has its own instance.i just had a tooth pulled yesterday and am under the influence of " " and cannot seem to part from learning while i should take some time off.Thanks again.

The second would only work if you had a function called yllow_cat that received "hunt", "fish", "sleep" as arguments and assigned them to members of an object. Like so:
function yllow_cat (a, b, c) {   this.sport = a;   this.food = b;   this.hobby = c;}

Both are legal. A third way is to create an "object literal":

p={firstname: "john", lastname: "Doe"};

A lot of this is preference. Note that the first and third techniques can not be re-used as written, where the second can create an arbitrary number of objects.

Link to comment
Share on other sites

From Wikipedia:

In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, strings, and booleans; some also have notations for elements of enumerated types and compound values such as arrays, records, and objects.In contrast to literals, variables or constants are symbols that can take on one of a class of fixed values. . .
A string literal looks like this: "Hello." You can assign it to a variable like this:str = "Hello";You can call a function like alert with a literal or a variable:alert(str);oralert("Hello");A literal is often used because it is simpler, but only if the value will never change. A variable is used when the value might be changed, or if the same value will be used multiple times. In long, complicated functions, the overuse of string literals and so-called "magic numbers" can make maintenance a real hassle. Many programmers use constants instead, defining them at the top of the program. JavaScript does not have true constants, but any variable can function as one.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...