One cool photo gallery

G

Grant Wagner

Richard said:
http://www.chunkysoup.net/advanced/oojavascript1/finishedgallery.html

This is by far the most unique galleries I've seen yet.
After seeing this, I know my idea can come together.

You're polluting the global namespace by defining your "JavaScript
object" "methods" outside the "class".

// object constructor
function csnPhotoObject(thumbID,thumbOffURI,thumbOnURI,fullID,fullURI) {
// code snipped
// attach object's methods
this.showThumb = csnPhotoObjectShowThumb;
this.hideThumb = csnPhotoObjectHideThumb;
this.showFull = csnPhotoObjectShowFull;
}
// define object's methods
// ALL THESE METHOD NAMES ARE IN THE GLOBAL
// NAMESPACE AND COULD BE OVERWRITTEN BY
// ANOTHER LIBRARY OF CODE. THEY CAN ALSO
// BE CALLED DIRECTLY, WHERE REFERENCES TO
// -this- IN THEM WOULD REFER TO THE GLOBAL
// OBJECT, PROBABLY NOT WHAT YOU INTENDED
function csnPhotoObjectShowThumb() { /* code snipped */ }
function csnPhotoObjectHideThumb() { /* code snipped */ }
function csnPhotoObjectShowFull() { /* code snipped */ }

Perhaps what you wanted is:

function MyObject(...params...) {
var param1 = param1;
var param2 = param2;
this.method1 = function() { /* code */ }
this.method2 = function() { /* code */ }
}
--- OR ---
function MyObject(...params...) {
this.param1 = param1;
this.param2 = param2;
}
MyObject.prototype.method1 = function() { /* code */ }
MyObject.prototype.method2 = function() { /* code */ }

At least with the above examples, I've encapsulated the "methods" that
belong to the "class" within the "class" and reduced polluting the
global namespace with all those functions. It also makes it much harder
for another library of code or JavaScript author to trample all over my
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top