473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
6 1394
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bruce W...1 | last post by:
A scripting newbie question... I'm trying to understand some code I found. This script conducts a poll and writes the results to a text file. The following statement is part of the source file. ...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
3
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can...
3
by: herrcho | last post by:
Here is the code.. #define NAME "MEGATHINK, INC" #define ADDRESS "10 Megabuck Plaza" #define PLACE "Megapolis, CA 94904" int main() { starbar(); printf("%s\n",NAME);
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
15
by: Brian Haynes | last post by:
I am having a problem with overloading. An example: Public Class Base ... End Class Public Class Derived Inherits Base ... End Class
8
by: razael1 | last post by:
I am putting debugging messages into my program by putting blocks that look like this: #ifdef DEBUG errlog << "Here is some information"; #endif All these #ifdef blocks make the code bulky and...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
4
by: BSand0764 | last post by:
Apologies for the length of this message, but I'm having problems getting an alternate function to be executed via a functor implementation. I have two classes (BkgLand and BkgWater) that...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.