473,396 Members | 2,036 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,396 software developers and data experts.

using methods in different scopes

Here's an example of 3 ways of using a method of an object with a
different object context. Is there a signficant difference between
method 1., where a reference to beta's method is copied and 2., where I
use call? To me no. 1 was a bit suprising, I expected that
alpha.introduce will point to beta.introduce, but will not change the
context of the function.
//simple object
var alpha={name:'alfa'}

//simple object, but can introduce itself
var beta={name:'beta',introduce:function() {alert(this.name)}}

//introduce beta
beta.introduce()

//1. alpha learns from beta how to introduce
alpha.introduce=beta.introduce
alpha.introduce()

//2.introduce alfa with a 'borrowed' method
beta.introduce.call(alpha)

//3.introduce alpha with a copied method
alpha.introduce=function() {return beta.introduce}()
alpha.introduce()
Sep 27 '07 #1
4 1309
On Sep 27, 3:06 pm, Karol Kowcik <kow_usun_...@gmail.comwrote:
Here's an example of 3 ways of using a method of an object with a
different object context. Is there a signficant difference between
method 1., where a reference to beta's method is copied and 2., where I
use call? To me no. 1 was a bit suprising, I expected that
alpha.introduce will point to beta.introduce, but will not change the
context of the function.

//simple object
var alpha={name:'alfa'}

//simple object, but can introduce itself
var beta={name:'beta',introduce:function() {alert(this.name)}}

//introduce beta
beta.introduce()

//1. alpha learns from beta how to introduce
alpha.introduce=beta.introduce
alpha.introduce()

//2.introduce alfa with a 'borrowed' method
beta.introduce.call(alpha)

//3.introduce alpha with a copied method
alpha.introduce=function() {return beta.introduce}()
alpha.introduce()
If a function is called as a property of an object, "this" always
refers to the object of which it is a property. Always, always,
always. A function can never be bound to one object with a "this"
reference of a different object. It doesn't work that way - that's
what Function.prototype.call and Function.prototype.apply are for.
You can consider those methods temporary "binders" which call a
function *as though* it were a property of the context object, while
leaving the context object itself intact. The difference being,
method 1 assigns a new property on your context object, while method 2
calls the function with the "this" context of the context object while
NOT assigning a new property to that object.

Method 3 then should be transparent - you're calling a function which
has a reference to a different object.

None of this has anything to do with "scope", a word that gets
transferred over from the Java/C++ world with none of the same
connotations. "Scope" in ECMAScript v3 refers to the resolving names,
and it exists at the function level, so a function has access to all
names that are in scope at its level or above, but no access to names
declared within functions inside of its scope. When resolving a name
used within a scope, the interpreter first checks for names defined
within the most local scope and proceeds outward to the global scope
on what you hear referred to as the "scope" chain. The "this" keyword
has no influence on scope, only on properties of objects, whose scope
is tied to the parent object's scope.

Hope that's clear as mud now!

-David

Sep 27 '07 #2
On Sep 27, 3:06 pm, Karol Kowcik <kow_usun_...@gmail.comwrote:
Here's an example of 3 ways of using a method of an object with a
different object context. Is there a signficant difference between
method 1., where a reference to beta's method is copied and 2., where I
use call? To me no. 1 was a bit suprising, I expected that
alpha.introduce will point to beta.introduce, but will not change the
context of the function.

//simple object
var alpha={name:'alfa'}

//simple object, but can introduce itself
var beta={name:'beta',introduce:function() {alert(this.name)}}

//introduce beta
beta.introduce()

//1. alpha learns from beta how to introduce
alpha.introduce=beta.introduce
alpha.introduce()

//2.introduce alfa with a 'borrowed' method
beta.introduce.call(alpha)

//3.introduce alpha with a copied method
alpha.introduce=function() {return beta.introduce}()
alpha.introduce()
If a function is called as a property of an object, "this" always
refers to the object of which it is a property. Always, always,
always. A function can never be bound to one object with a "this"
reference of a different object. It doesn't work that way - that's
what Function.prototype.call and Function.prototype.apply are for.
You can consider those methods temporary "binders" which call a
function *as though* it were a property of the context object, while
leaving the context object itself intact. The difference being,
method 1 assigns a new property on your context object, while method 2
calls the function with the "this" context of the context object while
NOT assigning a new property to that object.

Method 3 then should be transparent - you're calling a function which
has a reference to a different object.

None of this has anything to do with "scope", a word that gets
transferred over from the Java/C++ world with none of the same
connotations. "Scope" in ECMAScript v3 refers to the resolving names,
and it exists at the function level, so a function has access to all
names that are in scope at its level or above, but no access to names
declared within functions inside of its scope. When resolving a name
used within a scope, the interpreter first checks for names defined
within the most local scope and proceeds outward to the global scope
on what you hear referred to as the "scope" chain. The "this" keyword
has no influence on scope, only on properties of objects, whose scope
is tied to the parent object's scope.

Hope that's clear as mud now!

-David

Sep 27 '07 #3
On Sep 27, 3:06 pm, Karol Kowcik <kow_usun_...@gmail.comwrote:
Here's an example of 3 ways of using a method of an object with a
different object context. Is there a signficant difference between
method 1., where a reference to beta's method is copied and 2., where I
use call? To me no. 1 was a bit suprising, I expected that
alpha.introduce will point to beta.introduce, but will not change the
context of the function.

//simple object
var alpha={name:'alfa'}

//simple object, but can introduce itself
var beta={name:'beta',introduce:function() {alert(this.name)}}

//introduce beta
beta.introduce()

//1. alpha learns from beta how to introduce
alpha.introduce=beta.introduce
alpha.introduce()

//2.introduce alfa with a 'borrowed' method
beta.introduce.call(alpha)

//3.introduce alpha with a copied method
alpha.introduce=function() {return beta.introduce}()
alpha.introduce()
If a function is called as a property of an object, "this" always
refers to the object of which it is a property. Always, always,
always. A function can never be bound to one object with a "this"
reference of a different object. It doesn't work that way - that's
what Function.prototype.call and Function.prototype.apply are for.
You can consider those methods temporary "binders" which call a
function *as though* it were a property of the context object, while
leaving the context object itself intact. The difference being,
method 1 assigns a new property on your context object, while method 2
calls the function with the "this" context of the context object while
NOT assigning a new property to that object.

Method 3 then should be transparent - you're calling a function which
has a reference to a different object.

None of this has anything to do with "scope", a word that gets
transferred over from the Java/C++ world with none of the same
connotations. "Scope" in ECMAScript v3 refers to the resolving names,
and it exists at the function level, so a function has access to all
names that are in scope at its level or above, but no access to names
declared within functions inside of its scope. When resolving a name
used within a scope, the interpreter first checks for names defined
within the most local scope and proceeds outward to the global scope
on what you hear referred to as the "scope" chain. The "this" keyword
has no influence on scope, only on properties of objects, whose scope
is tied to the parent object's scope.

Hope that's clear as mud now!

-David

Sep 27 '07 #4
David Golightly wrote:
On Sep 27, 3:06 pm, Karol Kowcik wrote:
<snip>
>//1. alpha learns from beta how to introduce
alpha.introduce=beta.introduce
alpha.introduce()

//2.introduce alfa with a 'borrowed' method
beta.introduce.call(alpha)

//3.introduce alpha with a copied method
alpha.introduce=function() {return beta.introduce}()
alpha.introduce()
<snip>
Method 3 then should be transparent - you're calling a
function which has a reference to a different object.
<snip>

The parenthesise at the end of the function expression in the assignment
makes 'method 3' no more than a pointlessly inefficient equivalent of
'method 1'.

Richard.

Sep 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
1
by: G Fernandes | last post by:
Scope is a property of identifiers and defines where they are visible in a source file. Why then do most writings on C also use the word "scope" to refer to a property of different points of a...
10
by: David Hirschfield | last post by:
Here's a strange concept that I don't really know how to implement, but I suspect can be implemented via descriptors or metaclasses somehow: I want a class that, when instantiated, only defines...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
1
by: Screenbert | last post by:
After finding nothing anywhere in google I am posting this so everyone can benefit by it. The formating is not pretty since I copied it from my word document, but you should benefit by it. ...
0
by: screenbert | last post by:
Managing DHCP Servers using C# They said it was impossible. It couldn't be done. But you can in fact manage DHCP servers using C#. This includes creating and deleting Scopes, SuperScopes,...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
65
by: Spiros Bousbouras | last post by:
Has anyone found that declaring variables register affected speed of execution ? If yes on what hardware and around which year ?
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.