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

JS Class not implementing in IE

Hey everyone,

I've been writing some classes and have a problem with one in
particular not implementing in IE.

Here's the code:

var Expand = function(){
this.childmenu = function(){
alert('drop');
}
this.init = function(){
var self = Expand;
document.getElementById('expImg1').onclick = self.childmenu;
}
}

window.onload = Expand.init;

can anyone tell me if I've missed something please ???

Oct 16 '06 #1
8 1218
DoomedLung wrote:
I've been writing some classes ...
In a language that doesn't have them?
var Expand = function(){
this.childmenu = function(){
alert('drop');
}
this.init = function(){
var self = Expand;
document.getElementById('expImg1').onclick = self.childmenu;
}
}

window.onload = Expand.init;
There won't be an init property because the function expression is never
executed. If the parentheses denoting a function call were appended:

var Expand = function() {
this.init = function() {/* ... */};
}();
^^
this would still fail as the this operator would reference the global
object.

Either use the new operator to call the internal [[Construct]] method of
the function expression:

var Expand = new function() {
this.init = function() {/* ... */};
}();

or change your approach to use an object literal:

var Expand = {
init: function() {/* ... */}
};

Which you would choose would depend on whether was any advantage to
establishing a new execution context.

Mike
Oct 16 '06 #2
Hey Mike,

Thanks for your reply... I have tried your latter recommendation and
this has fixed the problem. Forgive me for being naive but when the
keyword 'new' is appended before the keyword 'function' does this not
make this function a Singleton?

Oct 16 '06 #3
DoomedLung wrote:
Hey Mike,

Thanks for your reply... I have tried your latter recommendation and
this has fixed the problem. Forgive me for being naive but when the
keyword 'new' is appended before the keyword 'function' does this not
make this function a Singleton?
No, it makes it a mistake. It unnecessarily imposes the overhead of a
constructor on what should be a simple invocation. Leave the new out.

var instance = function () {
...
}();

http://javascript.crockford.com/
Oct 16 '06 #4
Hey Douglas, thank you for your reply...

I have tried it your way and it doesn't work. I'm going to have to
rewrite the code so it can be implemented without the 'new' keyword.

Thanks again :)

Oct 16 '06 #5
Hey Douglas, thank you for your reply...

I have tried it your way and it doesn't work. I'm going to have to
rewrite the code so it can be implemented without the 'new' keyword.

Thanks again :)

Oct 16 '06 #6
DoomedLung wrote:
Hey Douglas, thank you for your reply...
Please quote appropriately when replying.
I have tried it your way and it doesn't work.
It will if you return an object. It's the way I'd normally create an
interface with "private" data or functions (rather than using the new
operator), but I didn't mention it for some reason.

var Expand = function() {
return {
init: function() {/* ... */}
};
}();

Again, unless there's some reason to introduce that extra execution
context, there's not much point in using it rather than using a simple
object literal. For example, if your childmenu method wouldn't be called
from the interface, it could be declared within the enclosing function
expression making it "private":

var Expand = function() {
function childMenu() {
/* ... */
}

return {
init: function() {
var element;

if (document.getElementById
&& (element = document.getElementById('expImg1')) {
element.onclick = childMenu;
}
}
};
}();

Mike
Oct 16 '06 #7
Thanks again :)

Oct 16 '06 #8
VK
DoomedLung wrote:
I have tried it your way and it doesn't work. I'm going to have to
rewrite the code so it can be implemented without the 'new' keyword.
If you just like to have "new" keyword used in your code :-) you can
also use anonymous object's method call:

function Expand() {
this.init = function() {
window.alert('Hi from anonymous object!');
}
}

window.onload = (new Expand).init;

Oct 16 '06 #9

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

Similar topics

1
by: MKoleoso | last post by:
Problem: C#- Unable to create instance of a class implementing from an interface I have: namespace someNamespace { public __gc class SomeClass1 { }
1
by: Maurice | last post by:
Hi, We are implementing some wrappers in C++ according to the Adapter Pattern. The classes and their methods in the Adaptee classes (open-source library) have already the interface that we like,...
2
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an...
9
by: Codex Twin | last post by:
Hi I have a common model for a Data Access Layer scenario. I have an abstract base class, called DalBase which contains a list of abstract methods. Lets call them: public abstract void Shine();...
3
by: Frans Bouma | last post by:
Hi, I have a serious problem with VB.NET and a DataTable derived class and I can't figure out how to solve it. I have implemented it in C# where it works perfectly, but I can't port one...
20
by: Scott M. | last post by:
What are the advantages of defining a class as part of another class definition (nesting classes)?
2
by: Kevin Frey | last post by:
Consider this simple example: interface IReader { bool Read( ); }; class MyReader : IReader { bool Read( ); // or should it be bool IReader.Read( ) ?
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
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
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
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
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.