Cylix wrote:
<snip>
myObject.prototype.linkFade = function(link, doShow) { with (this) {
...
...
...
}
Anyone can explain this for me?
Explain what exactly? A function expression is evaluated and a
reference to the resulting function object is assigned to a property of
the - prototype - of another function. if the - myObject - function is
used with the - new - operator the returned object will have its
internal [[Prototype]] property set to a reference to -
myObject.prototype - and as the chain of object references linked from
the internal [[Prototype]] property is used for property name
resolution that new object will behave as if it has a property named
'linkFade' that is a reference to the function object created with the
function expression.
what 'this' refer to? Is it refer to myObject?
The value of the - this - keyword within a function is determined
_only_ by how the function is called. If it is called as a property of
an object then - this - will be a reference to that object, otherwise
it will be a reference to the global object.
In ECMA 262 terms: a function is only executed with the - this -
keyword refering to an object other than the global object when the
property accessor (or whatever else is the right hand side of the
arguments list) resolves to an instance of the internal Reference type
that has a non-null 'base' property, and the object referred to by its
'base' property is also not an 'Activation' object (in either of those
case the - this - keyword will refer to the global object if employed
within the function).
If you do:-
var instanceObj = new myObject();
- then:-
instanceObj.linkFade();
- will result in the - this - keyword referring to the created object
instance as the property accessor - instanceObj.linkFade - is evaluated
into a Reference type with a reference to - instanceObj - as its 'base'
property, and:-
(x = instanceObj.linkFade)();
- will result in - this - referring to the global object (because the
assignment expression evaluates as the value assigned and that is a
function reference not a Reference type with -instanceObj - as its
'base' property.
It seems doesn't make sence...
<snip>
Javascript is a programming language so everything it does is governed
by relentless mechanical logic. As a result there is nothing that
javascript can do that cannot be understood or would not make sense.
Richard.