473,695 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on define function

I just know the form like:
myObject.protot ype.linkFade = function(link, doShow) {
....
....
....
}
-----------------------------------------------------------------------------------------------------------------------------------
myObject.protot ype.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 1410
Cylix wrote:
<snip>
myObject.protot ype.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.protot ype - 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.lin kFade();

- will result in the - this - keyword referring to the created object
instance as the property accessor - instanceObj.lin kFade - is evaluated
into a Reference type with a reference to - instanceObj - as its 'base'
property, and:-

(x = instanceObj.lin kFade)();

- 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.protot ype.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.protot ype.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.protot ype.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.protot ype.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(construct or);
// the method itself is available as arguments.calle e 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.protot ype.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.protot ype.linkFade = function(link, doShow) {
...
...
...
}
-----------------------------------------------------------------------------------------------------------------------------------
myObject.protot ype.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.linkFa de(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
2311
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. The exact code is not important to my question so don't wrack your brain on this. if (isset($votingstep)) { function ShowTheStuff($item, $itemvoted, $graph_width, $graph_height) { $hector=count($itemvoted);$totalvotes=0;$in=0;$stepstr='';...
17
6639
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. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
3
1667
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 you please verify for me? Usually, I write function that is about 100 lines. I would need 20 parameters which they are all reference to global variable inside struct. For best optimization, it does not require to create stack frame because...
3
2236
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
1879
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 queueing ip fragments as struct ipq { struct ipq *next; /* linked list pointers */ struct list_head lru_list; /* lru list member */ u32 user; u32 saddr;
15
2524
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
2211
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 harder to read, so I'd like to do something where I put: errMsg("Here is some information");
24
3445
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
4
1378
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 comprise a portion of a much larger simulation. These classes exist in separate libraries although the library that class BkgWater is in links in the library containing the BkgLand class (sounds confusing I know). In the simulation architecture that...
12
3011
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
0
8635
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8574
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9116
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8850
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8829
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6493
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1977
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.