First read http://www.w3schools.com/js/js_object_prototypes.asp
about prototypes and experiment with them in included exercises. Important to
understand that compared to the old typical object-oriented languages (VB, C++,
C#, Java, etc.) that are strongly typed and have such forms as objects being created
during the compilation time – dynamic languages (such as Perl, Ruby, Smalltalk,
and… JavaScript, along with the number of others) allow to change the program
and its data types during the execution depending on the situation. For example
var x = 5; dynamically becomes and integer, but if later you use x=”hello”; then
it becomes a string. You saw in the
examples from w3schools how it is possible to change, add, delete properties
and methods in objects. Same is possible to do in constructors using prototypes.
This allows to change whole groups (types of objects) like instead of changing
properties of a student Bob – you can change properties of the constructor
which changes properties of ALL students that were derived (using “new”) from
that constructor. For example in games, objects operating on the screen might
change properties (like colors, shapes, etc.) and behaviors under various conditions DURING
the game. In general program can change itself depending on the situation and
an old saying that computer can do ONLY what it was programmed to do is
incorrect. Software systems can LEARN (using various learning algorithms) and
evolve to become more adequate to the situation when after a while the creators
might not even know how and why the system does some new tricks.
This leads to the situation when in systems that were intended to be
dynamic – the programmers do not know what properties and how many of them
their objects will have after a while
depending on various situations. This leads to the need in using safer
forms of handling objects where their properties are treated as arrays and are
accessed in a loop like is shown in http://www.w3schools.com/js/tryit.asp?filename=tryjs_object_for_in
where the code uses it:
var txt = "";
var person = {fname:"John", lname:"Doe", age:25};
var x;
for (x in person) {
txt += person[x] + "
";}
Here x (better would have
been to use conventional i, j as indexes) is just an index reflecting the
position of an attribute in the object “person”. Using a tricky construction (for (x in person)) we can enumerate the
properties and then treat them like an array using their relative number in the
enumerated sequence treating it as person[x]
instead of giving a name of each property as you did before.
An example of how useful objects are in making your code more compact and logic more clear can be seen
in operating whole objects as variables by passing them to other objects or
functions:
var father = {
firstname:"John",
lastname:"Doe",
age:65,
eyecolor:"blue"
};
function canRetire (person){
if (person.age > 62) {
return true;
return true;
} else {
return false;
}
Here, assuming that object father
is an instance (derived from) of the type (class) person, we are passing the whole object to the function checking if
that particular person (particular object) can retire. In a similar fashion
once a function gets an object as a parameter – it can internally use all its
properties and functions. Notice that instead of sending a bunch or parameters
to a function we are sending just one (of type person) that can have a very
complex and rich structure.
No comments:
Post a Comment