pa***********@l ibero.it wrote:
VK ha scritto:
>pa***********@l ibero.it wrote:
>>What that syntax "var busyBox = new ... " actually means ?? What is
NEW.
new ConstructorName () means "make me a new instance of this
kind of object and return a reference to this instance".
It would be more accurate to say that his creates a new instance of a
javascript object and then performs a number of (implied or direct)
actions upon that object. The javascript object is dynamic and can have
properties added to it at any time. Constructor functions are just a
mechanism for having like changes applied to new Objects, so that the
resulting objects can be regarded as being of some 'type' or 'class'.
This is really nice. I didn't get that it is possible to "create"
objects in javascript. Beautiful!
It is actually almost impossible not to create objects in javascript.
Presumably you mean objects of some programmer defined 'type' or
'class'.
I see you defined fields. Is it possible to define also "methods" and
"properties " like in .net?
Methods are just references to functions assigned to properties of
objects _and_ called as through property accessors (as the - this -
value in a function is determined _only_ by how the function is called.
That is, there is no inherent relationship between a function that is
used as a method and any object instance that is using it.).
And don't tell me we also have inheritance or interfaces! Do we?
<snip>
Native inheritance is through the prototype chain. An object instance
is made into the prototype of any new object, usually by assigning a
reference to that object to the - prototype - property of the
constructor (before it is use to create new objects) or by modifying
the object that is already the default prototype of the constructor. If
an attempt is made to look up properties on an object and that object
does not have such properties itself its prototype is examined to see
if it has such a property and if it does the value of that property is
used (else the prototype's prototype is examined (if it has one)).
Thus the object on the prototype of a newly created object provide
default values (including default methods) for that object. Once a
different value is written to the object itself the value on objects on
the prototype chain are 'masked' and their values will no longer be
returned if the value of the property is read from the object instance.
The prototype chain cannot branch (it is a chain not a tree) so there
is no equivalent to multiple inheritance in javascript (though the
concept can be implemented once an appreciation of 'classes' in
javascript not being a native characteristic of the language but
instead being a programmer imposed design concept that is manifest in
making similar modifications to instances of the single native object
'class'.
Javascript's prototype inheritance usually resembles something like:-
function Dell() {
;
}
Dell.prototype. producer = 'Dell, Inc.';
Dell.prototype. URL = 'http://www.dell.com';
function OptiFlex (processor, memory) {
if(processor){
this.processor = processor;
}
if(memory){
this.memory = memory;
}
}
OptiFlex.protot ype = new Dell();
OptiFlex.protot ype.processor = 'Athlon 1.2GHz';
OptiFlex.protot ype.memory = '128Mb';
As there are no 'classes' in javascript and all objects are the
actually the same type of object, just modified in differing ways, the
sense in which an interface is defined is only multiple objects having
the sharing a sub-set of defined methods and properties. It is trivial
to implement that.
Because the objects in javascript are dynamic, in addition to defining
the like-sub-set of properties and methods that might be called an
interface on groups of 'classes', and interface may be added to an
object. For example, suppose an interface known as "X" defined - get -,
- put - and - size - methods, a factory function could be used that
would take an object of any 'class' as its argument and add the
interface to the object:-
function augmentWithX_In terface(obj){
var storage = [];
obj.get = function(key){
...
};
obj.put = function(key, value){
...
};
obj.size = fucntion(){
...
};
return obj;
}
var dellWithX = augmentWithX_In terface( new Dell() );
(Note: this example is closure-based in its mechanism so it is
something you will not want to even consider using until you fully
understand prototype inheritance).
Richard.