467,076 Members | 933 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,076 developers. It's quick & easy.

Question on define function

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.

Aug 4 '06 #1
  • viewed: 1231
Share:
6 Replies
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.

Aug 4 '06 #2
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.

Aug 4 '06 #3
Cylix wrote:
<snip>
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.

Aug 4 '06 #4
VK
Cylix wrote:
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 :-)

Aug 4 '06 #5
VK wrote:
<snip>
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.

<snip>
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.

Aug 4 '06 #6
Ray

Cylix wrote:
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.
>
Sorry for asking newbie question again.
Thanks for help.
Aug 5 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bruce W...1 | last post: by
17 posts views Thread by Medi Montaseri | last post: by
3 posts views Thread by Bryan Parkoff | last post: by
3 posts views Thread by herrcho | last post: by
1 post views Thread by rahul8143@gmail.com | last post: by
15 posts views Thread by Brian Haynes | last post: by
8 posts views Thread by razael1@gmail.com | last post: by
24 posts views Thread by Michael | last post: by
4 posts views Thread by BSand0764@msn.com | last post: by
12 posts views Thread by Bryan Parkoff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.