On 28/03/2006 19:18,
petermichaux@gmail.com wrote:
[color=blue]
> VK wrote:
>[color=green]
>>
petermichaux@gmail.com wrote:
>>[color=darkred]
>>> If I do the following...
>>>
>>> var a = document.getElementById("my_div");
>>>
>>> var b = new Object();
>>> b.prototype = a;
>>> b.new_property = function(){alert("the new property!");
>>>
>>> Now b has the new property.[/color][/color][/color]
Yes, it does.
[color=blue][color=green][color=darkred]
>>> Also, b has all the properties of a.[/color][/color][/color]
No, it doesn't. Firstly, the prototype property is only significant for
constructor functions. When an object is created using that function and
the new operator, the object referenced by the prototype property of the
constructor is assigned to the internal [[prototype]] property of the
newly created object. It is this internal property that is used when
trying to lookup an object property. See section 8.6.2.1 [[Get]](P) in
ECMA-262. The new object will not have a prototype property.
[snip]
[color=blue][color=green]
>> a is a reference to a DOM object (presumably DIV), b is a reference
>> to JavaScript object. They are completely different species.[/color]
>
> Say what?! This sounds extremely strange.[/color]
The next thing to note is that there are two broad types of object:
native objects, and host objects.
Native objects are specified by ECMA-262 and include Object, Function,
Array, Math, String, Number, Date, and RegExp. The objects in that list
are also known as built-in objects, as they are available from the
outset of execution. User-defined objects (created using constructor
functions) are also native objects.
Host objects are provided by the environment. All DOM objects are host
objects, as are the 'DOM 0' objects such as location. In IE, ActiveX
objects are also of this category.
Whilst native objects must act as described by ECMA-262, host objects
are at the whim of the implementation in many regards. For instance, in
Gecko browsers, DOM objects have prototype properties that can be
modified to affect relevant DOM nodes, and DOM objects can be the
prototype object of constructor functions. In IE, neither is the case,
but this is permitted.
[snip]
Hope that helps,
Mike
--
Michael Winter
Prefix subject with [News] before replying by e-mail.