Placing Object Inside of Another Object

V

vunet

I used to have an object written in this format:

var obj = new Object();
obj.prop = "blah";
obj.myFunction = function(){
//something here
}
obj.myFunction.prototype = {
id1:'',
id2:function{

}
//etc
}

Now I try to place obj into another object but I have "missing : after
property id" error in prototype line. Could someone correct? I am not
much used to this format so I am getting confused.

var superParentObj = {
parentObj : {
prop = "blah";
myFunction : function(){
//something here
},
myFunction.prototype = { //<===== missing : after property
id
id1:'',
id2:function{
//...
}
//etc
}
}
}
 
R

RobG

I used to have an object written in this format:

var obj = new Object();
obj.prop = "blah";
obj.myFunction = function(){
    //something here}

obj.myFunction.prototype = {
    id1:'',
    id2:function{

    }
    //etc

}

Now I try to place obj into another object but I have "missing : after
property id" error in prototype line. Could someone correct? I am not
much used to this format so I am getting confused.

var superParentObj = {
     parentObj : {
         prop = "blah";

What is expected here is object literal notation, not an assignment,
use:

prop: "blah";

         myFunction : function(){
                 //something here
         },
         myFunction.prototype = {  //<===== missing : after property
id

Same here. You can do:

var obj = {
prop: "blah",
myFunction: function(){}
}

obj.myFunction.prototype = {
id1: 'foo',
fn: function(){alert(this.id1);}
}

var x = new obj.myFunction();
x.fn();
 
V

vunet

What is expected here is object literal notation, not an assignment,
use:

prop: "blah";


Same here. You can do:

var obj = {
prop: "blah",
myFunction: function(){}

}

obj.myFunction.prototype = {
id1: 'foo',
fn: function(){alert(this.id1);}

}

var x = new obj.myFunction();
x.fn();

Thank you. This as I see is bringing prototype outside of the parent
objects. Can I stay with my current version where error occurs but fix
it in there? Or, in other words, how to place initial code below
inside of the parent object?

var parentObj = {
//how to place "var obj" here?????
}

var obj = new Object();
obj.prop = "blah";
obj.myFunction = function(){
//something here
}
obj.myFunction.prototype = {
id1:'',
id2:function{}
}

Thank you. I just really wish I can move one object inside of another
if there is a way. I need it for the library.
 
R

RobG

Thank you. This as I see is bringing prototype outside of the parent
objects.

You can only attach a prototype to a function object that has either
been declared or initialised:

e.g.

function Foo(){}
// The assignment to Foo.prototpye can be anywhere, but
// is ususally immediately after the declaration
Foo.prototpye = ...

var Bar = function(){}
// this must be after the initialiser, as assignment
// only occurs when the code is executed
Bar.prototpye = ...
Can I stay with my current version where error occurs but fix
it in there? Or, in other words, how to place initial code below
inside of the parent object?

No. You can't do a function declaration or initialisation as part of
an object literal, only by assignment to an object property.

var parentObj = {
//how to place "var obj" here?????
}

var obj = new Object();
obj.prop = "blah";
obj.myFunction = function(){
//something here}

obj.myFunction.prototype = {
id1:'',
id2:function{}

}

You could do it like this (though it is likely a horrible abuse of the
'private member' pattern):

var parentObject = (function(){

function F(){}

F.prototype = {
id1: '',
id2: function(){
alert('fn on prototype');
}
};

var g = new F();

return {
prop: 'blah',
myFunction: function(){g.id2()}
};
})();

parentObject.myFunction();
 
R

RobG

You can only attach a prototype to a function object that has either
been declared or initialised:

e.g.

function Foo(){}
// The assignment to Foo.prototpye can be anywhere, but
// is ususally immediately after the declaration
Foo.prototpye = ...

var Bar = function(){}
// this must be after the initialiser, as assignment
// only occurs when the code is executed
Bar.prototpye = ...


No. You can't do a function declaration or initialisation as part of
an object literal, only by assignment to an object property.

Anyhow, maybe the following is more what you're after:

var superParentObj = {
parentObj: {
prop: 'blah',
myFunction: (function(){
function F(){}
F.prototype = {
id1: '',
id2: function(){alert('id2...');}
}
return new F();
})()
}
}

superParentObj.parentObj.myFunction.id2();


However is seems somewhat obfuscated.
 
L

Lasse Reichstein Nielsen

RobG said:
However is seems somewhat obfuscated.

Or if one does not object to extending Function.prototype:

Function.prototype.extend = function(obj) {
for(var i in obj) {
this.prototype = obj;
}
return this;
}

var obj = {
prop: "blah",
myFunction : function(){}.extend({
id1: 'foo',
fn: function(){ alert(this.id1); }
})
};

(new obj.myFunction()).fn();

/L
 
L

Logos

You can only attach a prototype to a function object that has either
been declared or initialised:

e.g.

function Foo(){}
// The assignment to Foo.prototpye can be anywhere, but
// is ususally immediately after the declaration
Foo.prototpye = ...

var Bar = function(){}
// this must be after the initialiser, as assignment
// only occurs when the code is executed
Bar.prototpye = ...


No. You can't do a function declaration or initialisation as part of
an object literal, only by assignment to an object property.





You could do it like this (though it is likely a horrible abuse of the
'private member' pattern):

var parentObject = (function(){

function F(){}

F.prototype = {
id1: '',
id2: function(){
alert('fn on prototype');
}
};

var g = new F();

return {
prop: 'blah',
myFunction: function(){g.id2()}
};

})();

parentObject.myFunction();

:shudder:
 
V

vunet

Anyhow, maybe the following is more what you're after:

var superParentObj = {
parentObj: {
prop: 'blah',
myFunction: (function(){
function F(){}
F.prototype = {
id1: '',
id2: function(){alert('id2...');}
}
return new F();
})()
}

}

superParentObj.parentObj.myFunction.id2();

However is seems somewhat obfuscated.

Thank you. I did follow your first suggestion.
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top