Question on define function

C

Cylix

I just know the form like:
myObject.prototype.linkFade = function(link, doShow) {
....
....
....
}
-----------------------------------------------------------------------------------------------------------------------------------
myObject.prototype.linkFade = function(link, doShow) { with (this) {
....
....
....
}

Anyone can explain this for me?
what 'this' refer to? Is it refer to myObject? It seems doesn't make
sence...

Sorry for asking newbie question again.
Thanks for help.
 
R

Richard Cornford

Cylix wrote:
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.
 
C

Cylix

Thanks, Richard.
I do trust that you are expert in ECMA.

I better clearize my question:
myObject.prototype.linkFade = function(link, doShow) { with (this) {

^^^^^^^^^^^^ What is the meaning of " with (this) " ?

Thank you so much.
 
R

Richard Cornford

Cylix wrote:
I better clearize my question:

Does that mean that your question was not related to either the subject
you used or 90% of the code you posted?
myObject.prototype.linkFade = function(link, doShow) { with (this) {

^^^^^^^^^^^^ What is the meaning of " with (this) " ?

The expression in the parentheses of the - with - statement is
evaluated, type-converted into an object (if necessary) and added to
the top of the scope chain for the current execution context. Then a
statement (which may be a Block statement: braces containing any number
of other statements) is evaluated and then the object added to the
scope chain is removed.

The effect of this is that Identifiers resolved in the statement(s)
executed while the object is on the scope chin will consider properties
of that object to see if their names correspond with the character
sequence in the Identifiers. And as the object is at the top of the
scope chain it is considered first, so its properties can mask other
properties on the scope chain. In addition, any function expressions
evaluated within the statement(s) executed while the object is on the
scope chain will have its internal [[Scope]] property set to the scope
chain in force at the moment of its creation, the one with the added
object at the top.

Generally, the use of the - with - statement is stongly discouraged as
it results in obscure code and causes confusion when people find that
they can use unqualified Identifiers to read the properties of the
object added to the scope chain but they cannot use them to create new
properties of that object.

Richard.
 
V

VK

Cylix said:
myObject.prototype.linkFade = function(link, doShow) { with (this) {

^^^^^^^^^^^^ What is the meaning of " with (this) " ?

Thank you so much.

<html>
<head>
<title>Demo</title>
<script type="text/javascript">

// myObject is a constructor:
function myObject() {
this.name = 'myObject';
}


// creating an instance of myObject object:
var obj = new myObject();


// Adding the 'linFade' method to the myObject prototype
// makes it available to all instances of myObject objects,
// including those that have already been created.
myObject.prototype.linkFade = function(link, doShow) {
with(this) {
// this refers to the instance of myObject (obj object):
alert(name);
// constructor property of obj object refers to the
// constructor of the current instance (myObject):
alert(constructor);
// the method itself is available as arguments.callee property:
alert(arguments.callee);
}
};

obj.linkFade();

</script>
</head>
<body>
</body>
</html>

There are some pecularities with /this/ value in JavaScript/JScript but
from the given fragment it is not clear is there is any actual problem.
If it was just out of curiosity then here is the answer :)
 
R

Richard Cornford

VK wrote:
myObject.prototype.linkFade = function(link, doShow) {
with(this) {
// this refers to the instance of myObject (obj object):

No it does not, it only _may_ refer to that object, depending on how
this function is called. This has been explained to you half a dozen
times by now, but apparently to no avail.

There are some pecularities with /this/ value in JavaScript/JScript ...
<snip>

No there are not, no javascript environments have been observed to
handle the - this - keyword in any way that differs from the
specification. There are peculiarities in your understanding of
javascript, but it appears that nothing can be done to fix that.

Richard.
 
R

Ray

Cylix said:
I just know the form like:
myObject.prototype.linkFade = function(link, doShow) {
...
...
...
}
-----------------------------------------------------------------------------------------------------------------------------------
myObject.prototype.linkFade = function(link, doShow) { with (this) {
...
...
...
}

Anyone can explain this for me?
what 'this' refer to? Is it refer to myObject? It seems doesn't make
sence...

In this example, yeah, "this" always refers to the object on which the
function is called. So when you say

myObject.linkFade(mylink, true);

the "this" inside the function you've assigned to linkFade refers to
myObject.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top