View Full Version : Help with Classes and OOP Js
C.O.D.E.N.A.M.E
03-25-2010, 09:40 PM
jQuery(document).ready(function() {
// equal height boxes
var max_height = 0;
$('.boxes').each(function(){
max_height = Math.max(max_height, $(this).height());
});
$('.boxes).css({
'min-height':max_height,
'height':'auto'});
if ($.browser.msie && $.browser.version.substr(0,1)<7) {
$('.boxes').css('height', max_height);
}
});
Better said... How can I rewrite this function as a Class? That I can just re-use the code using as object and methods? To avoid redeclaring the whole thing every time.
Dormilich
03-25-2010, 10:09 PM
this code is already OOP JS.
[PHP]
Better said... How can I rewrite this function as a Class? That I can just re-use the code using as object and methods? To avoid redeclaring the whole thing every time.
JavaScript has no Classes nor Inheritance. JavaScript is a prototype based language, not a class based one. For JavaScript everything is an object, and objects' properties are passed from object to its prototype by delegation, not by inheritance.
On the other hand you are dealing with a library: JQuery. It is hard to modify library's codes for many reasons: libraries use custom methods, custom methods are hard to be found in the code, modifying them might provoke unexpected errors within some other dependent methods or objects, and so on... That means we might explain you how to use a constructor to do what you need, but not over a library's obscure code.
Dormilich
03-26-2010, 12:54 PM
JavaScript has no Classes nor Inheritance.
I wouldn’t say JavaScript has no Inheritance (although that depends on how strict you define Inheritance). otherwise you wouldn’t have the possibility to extend the Interfaces.
I wouldn’t say JavaScript has no Inheritance (although that depends on how strict you define Inheritance). otherwise you wouldn’t have the possibility to extend the Interfaces.
Well yes, we are talking about inheritance but in Javascript the "inheritance" is performed via a process of cloning the existing objects - that's the difference.
We may also notice that in javascript there is no separation between "classes" and "instances", so that there are no real "subclasses nor "superclasses", thus there is nothing to inherit:)
Dormilich
03-26-2010, 01:25 PM
We may also notice that in javascript there is no separation between "classes" and "instances", so that there are no real "subclasses nor "superclasses", thus there is nothing to inherit:)
didn’t you say there are no Classes at all? *gg* although you could say, there are "Superobjects" (like Object, Array or Node). and much of the DOM is based on inheritance, too.
Let's say that there is a class-based inheritance and a prototype-based inheritance. The second is based on a delegation chain constructor<->prototype.
In fact we may simulate a class-based inheritance in JavaScript, but for that we might need a sort of extended function, something like:
function extend(subclass,superclass){
function f(){}
f.prototype = superclass.prototype;
subclass.prototype = new f();
subclass.prototype.constructor = subclass;
subclass.superclass = superclass;
subclass.superproto = superclass.prototype;
if (superclass.prototype.constructor == Object.prototype.constructor) {
superclass.prototype.constructor = superclass;
}
}
If I am not wrong, most of the libraries have such a method, in order to mime a class-based inheritance
Dormilich
03-26-2010, 01:45 PM
Let's say that there is a class-based inheritance and a prototype-based inheritance. The second is based on a delegation chain constructor<->prototype.
I think that’s a good point to agree on.
PS. that code snippet looks somewhat familiar …
I think that’s a good point to agree on.
PS. that code snippet looks somewhat familiar …
It's a piece of code slightly adapted from Yahoo's YUI, several years ago, if I well remember. I liked the idea and kept it.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.