Re: best way to inherit funcs from another object
- From: RobG <robgbne@xxxxxxxxx>
- Date: Wed, 8 Jul 2009 19:36:13 -0700 (PDT)
On Jul 8, 11:58 pm, Stevo <n...@xxxxxxxxxxxx> wrote:
RobG wrote:
To use prototype inheritance (you probably need something a bit
smarter, but this will do for now):
// Declare Libv4 as a function (add stuff to the body to initialise
instances):
function Libv4(obj) {
for (var p in obj) {
this[p] = obj[p];
}
};
Libv4.prototype = {
func1: function(){...},
func2: function(){...},
func3: function(){...}
};
// Create an instance and call an inherited function
var obj123 = new Libv4(myobj123);
obj123.func1();
--
Rob
Based on the above, I've restructured things and got it basically
working and still meeting the initial requirements of keeping the
functions and data object separate, with the data object being defined
first. Here it is:
if(!window.Libv4){
Since you are dealing with native objects use typeof, not simple
boolean conversion. You seem to want fully qualified paths to native
objects so don't use window - get a reference to the global object and
use that:
var globalObj = (function(){return this;})();
if (typeof globalObj.Libv4 != 'function') {
Now you *know* that globalObj will be a reference to the global object
(which is effectively the same as the window object in browsers), you
don't have to worry that some other window identifier on the scope
chain has shadowed the one you wanted.
window.Libv4=function(i,n,r){
this.id=i; this.name=n; this.requires=r;
}}
I'd use something more like the generic constructor I posted above -
it doesn't care what the properties are, they are defined
independently (helps avoid name clashes) and the constructor just
copies them over. You may want to add a "hasOwnProperty" test to
filter out inherited, enumerable properties, there are issues with
that too:
<URL: http://groups.google.com/group/comp.lang.javascript/msg/786c00c19e7f3ab6
else document.write tag to load Libv4 js
That doesn't quite make sense to me - if it doesn't exist, you create
it. If it does exist, you load the script (presumably replacing what
is there)?
The simplest method is to load the scripts up front, then, in a
subsequent script element, use the loaded script. You can use a
scheme like:
<script ...>
// work out which scripts to load
// load using document.write
</script>
<!-- loaded scripts will end up here -->
<script...>
// Use loaded scripts
</script>
Whatever strategy you employ, you have to wait for the script to load
(possibly quite some time) and be executed (probably very quick)
before trying to use the code in it.
window.obj123=new Libv4(123,"me","Libv4");
If the Libv4 file is loaded then it runs the code from it shown here:
Libv4.prototype.func1=function(){};
Libv4.prototype.func2=function(){};
Libv4.prototype.func3=function(){};
Now a call to obj123.func1() works.
Yes, that's prototype inheritance. Since you seem to want to define
the data and function components of obj123 separately, you'll need a
scheme to prevent name clashes, maybe:
1. Prefix all functions with "fn_" and make it known it is reserved
for methods, or do something similar for data properties, or both.
2. Put the data on a data property like:
var obj123.data = {...};
Which is also helps if you want to do stuff with just the data part
(say pass a reference or replace it), without messing with the rest of
the object (which may not matter to you anyway).
--
Rob
.
- References:
- best way to inherit funcs from another object
- From: Stevo
- Re: best way to inherit funcs from another object
- From: RobG
- Re: best way to inherit funcs from another object
- From: Stevo
- best way to inherit funcs from another object
- Prev by Date: Re: single page apps, URL hash setting, bookmarking, & the back button.
- Next by Date: Re: single page apps, URL hash setting, bookmarking, & the back button.
- Previous by thread: Re: best way to inherit funcs from another object
- Next by thread: Fetch php in JavaScript (DOM)
- Index(es):