Re: Odd behavior involving function.apply()



The 'secret' is designed to illustrate that a hidden member could be
introduced into the created object using this pattern. It's based on
what I read on Crockford.com.

As for the intent, considering JavaScript really doesn't have "types",
I'm trying to create a new -object-. I'm obsessed lately with the idea
that JavaScript 1) was never designed to support classes (yet everyone
tries to force it to), 2) supports -prototypal- inheritance instead of
class-based inheritance, and 3) prototypes are -not- classes.

So the object() function is supposed to create either 1) objects with
an "instance" semantic attached to them, or 2) patterns -- prototypes
-- upon which other objects are based. But it should be composable --
again, the philosophy I'm adopting is that objects inherit from
prototypes, not classes/types. And, thusly, an object/instance created
from a prototype should be capable of acting as a prototype itself.

object() should be the root of this functionality: any source object,
by virtue of JavaScript, can be used as a prototype to create any other
object which can be said to "inherit" from the source object. I'm
trying to get back to basics amidst all the half-baked classical
inheritance implementations that do many things well but not the whole
thing with excellence. But though I'm getting back to basics, I still
want to try and introduce things like information hiding into the mix.

object() is a very hairy mutation of the function Crockford discusses
here: http://javascript.crockford.com/prototypal.html. I wasn't
satisfied with that method because it didn't support a more intuititve
initialization mechanism.

I had something I liked until I tried nested creation. In this
particular problem, I can't understand how the addition of one
property, subObject can so dramatically alter its owner object. I
thought I'd isolated all my execution contexts properly by latching
onto 'this' in all the right places using local variables, but somehow
the -nested- call to object() ended up modifying the object that
-owned- the call. How was that boundary violated?

As for it being a scope quagmire, well, in my defense, it -is-
JavaScript. Scope stopped being an easy concept when I was introduced
to the wild world of closures. :)

--
Jeff S.


Jeremy wrote:
Jeff Stewart wrote:
<snipped long, complicated test case>

Why doesn't this work more intuitively?


The best kind of irony is unintentional irony. That made me laugh out
loud :-)

As for your problem - your code looks like a scope quagmire to me.
Shouldn't you be assigning properties to your object rather than
creating them as local variables (as with "secret")? I don't fully
understand what you're trying to do - are you trying to create an
*object* that inherits from Prototype, or a new *type* that inherits
from Prototype?

Can you explain what you're trying to do? Maybe there's an analogous
pattern in a different language that you're trying to emulate? Whatever
it is, there is probably a simpler way.

Jeremy

.



Relevant Pages

  • Re: Terms for method types?
    ... this is just basic prototype ... on this stage - about the exact inheritance mechanics, ... var myInstance = new MyObject; ... First off, I was talking about javascript, not java. ...
    (comp.lang.javascript)
  • Re: Terms for method types?
    ... on this stage - about the exact inheritance mechanics, ... could freely use the regular English words like class, prototype, ... First off, I was talking about javascript, not java. ... In this case your understanding of the nature of the closure is not ...
    (comp.lang.javascript)
  • Re: JavaScript for grown-ups
    ... In javascript functions are objects, but that does not seem to be what ... When an assignment is made to a named property ... Where is .prototype defined? ... That is prototype based inheritance (as opposed ...
    (comp.lang.javascript)
  • Re: Replacing document.getElementById with $ (dollar sign)
    ... saved about 8KB of javascript. ... want to know whether there's any browser problems with doing this? ... Looking at Prototype JS: ... var methods = Object.clone, ...
    (comp.lang.javascript)
  • Re: OOP using javascipt
    ... You have no clue about the features of class-based inheritance, ... function ClassB(arg1, arg2) { ... // prototype object fix in case you want to identify Child objects later ...
    (comp.lang.javascript)