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

loosing scope/context in OOP

Tim
Hey, so I created my own function that works much like dojo.declare
for creating classes and implementing simple inheritance, example:

Class('Clock', Component, {

param: 12

constructor: function(){

},

method: function(){

},

obj: {
a: function(){
alert(this.param);
}
}

})

the problem is when I try to call the function "a" contained in "obj".
I try doing something like this:

var clock = new Clock();
clock.obj.a.call(clock);

but that doesn't work, it looses scope, it thinks this.param is also
contained in "obj" despite the fact that I am calling the function
with
the scope of "clock"???

Any help is appreciated.

btw, just so you know:
'Clock' is the class name
Component is the superclass
param, constructor, method, and obj are all added to the prototype of
'Clock'
Sep 13 '08 #1
4 1173
Tim wrote:
[...] I created my own function that works much like dojo.declare
for creating classes and implementing simple inheritance [...]
There are no classes. And `dojo.declare' is equally flawed, I suppose.
Learn to understand how prototype-based OOP works instead of trying to
impose class-based thinking on it.

<http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/JavaScript_Overview#JavaScript_and_Java>
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Sep 13 '08 #2
Tim
On Sep 13, 2:38*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Tim wrote:
[...] I created my own function that works much like dojo.declare
for creating classes and implementing simple inheritance [...]

There are no classes. *And `dojo.declare' is equally flawed, I suppose.
Learn to understand how prototype-based OOP works instead of trying to
impose class-based thinking on it.

<http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/JavaScript_...>

PointedEars
--
* * realism: * *HTML 4.01 Strict
* * evangelism: XHTML 1.0 Strict
* * madness: * *XHTML 1.1 as application/xhtml+xml
* * * * * * * * * * * * * * * * * * * * * * * * * * -- Bjoern Hoehrmann
My mistake, I realize there are no classes in Javascript and I meant
to say that it looks like dojo.declare not works like dojo.declare.
Forget the fact that I made my own function to create "classes" or
that I use a simple hack to "imitate" inheritance, basically all I
want to know is why the function won't execute in the context of which
I supply via call or apply?
Sep 13 '08 #3
Tim wrote:
Thomas 'PointedEars' Lahn wrote:
>Tim wrote:
>>[...] I created my own function that works much like dojo.declare
for creating classes and implementing simple inheritance [...]
There are no classes. And `dojo.declare' is equally flawed, I suppose.
Learn to understand how prototype-based OOP works instead of trying to
impose class-based thinking on it.

<http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/JavaScript_...>
[...]

[...]
Forget the fact that I made my own function to create "classes" or
that I use a simple hack to "imitate" inheritance, basically all I
want to know is why the function won't execute in the context of which
I supply via call or apply?
However, it cannot be ignored how you create your constructor because it
determines how the method in question is defined and called; it would
explain what you have observed. Start with a simple constructor and
prototype object instead:

function Clock()
{
this.answer = 42;
}

Clock.prototype.obj = {
a: function() {
window.alert(this.answer);
}
};

var clock = new Clock();

// alerts 42 (in conforming implementations)
clock.obj.a.call(clock);
PointedEars

P.S.
Please trim your quotes (and attribution line) to the minimum required
to retain context, and don't quote signatures.
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Sep 13 '08 #4
On Sat, 13 Sep 2008 at 20:38:27, in comp.lang.javascript, Thomas
'PointedEars' Lahn wrote:
>Tim wrote:
>[...] I created my own function that works much like dojo.declare
for creating classes and implementing simple inheritance [...]

There are no classes.
<snip>

Have you never wondered what ECMA 262 means when it says, in v3, section
15.4 :
"Array objects give special treatment to a certain class of
property names."
The word 'class' is being used with its ordinary English/mathematics
meaning. There's nothing wrong with a programmer using the word to
describe things in the program. There's also nothing wrong with writing
code to create objects belonging to a particular class.

What javascript hasn't got is class definitions written by the
programmer and constructor code written by the compiler.
That kind of language is often called 'class-based' but this is
misleading; it should really be called a 'class-definition-based'
language.

John
--
John Harris
Sep 15 '08 #5

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

Similar topics

4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
0
by: falcon | last post by:
I know I came after the battle. And I have just another sight on context managment. Simple Context Managment may look in Python 2.4.1 like this: Synhronized example: def...
5
by: My4 | last post by:
Dear Sirs, I want to give the user of my intranet a warning that if he uses the explorers right corner 'X' to close the browser he looses his information. I tried to prevent him from closing, or...
1
by: antonyliu2002 | last post by:
This message was originally a follow-up in a thread, but it went ignored and I do want some help so I am initiating it as a new topic. After Patrick and Karl showed me some examples, I was trying...
2
by: Phoe6 | last post by:
Hi all, I have this Code Context feature under Options in the IDLE. How should I use it? Are there folks here who use it regularly and find it useful. Please guide me. Thanks! Senthil
3
by: namewitheldbyrequest | last post by:
"The XML element 'EnableTheming' from namespace 'http://tempuri.org/' is already present in the current scope" I created a Web Service: I imported System.Data.SqlClient so I could access SQL...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: DanYan | last post by:
So I was doing some stuff in Javascript, and I want to get access to a function's scope chain. As a simplified example of what I actually am trying to do, suppose I have this: function add(b)...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.