1

I have created a "Namespace" of sorts using nested objects in Javascript and am trying to "new' up an instance of a javascript object.

//
// Create the ABC.DTO "namespace"
if (typeof (ABC) == 'undefined') var ABC= { DTO: {} };
//
// Define the ListType object
ABC.DTO.ListType = function (pId, pName) {

    var id   = pId;
    var name = pName;

    return {
        Id: id,
        Name: name
    }
};
//
// Create an instance of the "listType" object
var type1 = new ABC.DTO.ListType(1, 'Letter Type'); // THROWS ERROR

The error being thrown is "Object doesn't support this action" ... I have reviewed the following posts and, unless I am missing something I feel like the code is conformign correctly. Am I overlooking something?

Define a “nested” object constructor in JavaScript?

Instancing new objects in javascript

3
  • 1
    Shoulldn't it be ABC.DTO instead of GMCR.DTO? Commented May 20, 2013 at 16:03
  • I see a different error: Uncaught ReferenceError: GMCR is not defined. Commented May 20, 2013 at 16:06
  • Yeah that was a typo. I have fixed in in the OP. Sorry 'bout that. Commented May 20, 2013 at 16:33

1 Answer 1

0

This works in the most simple case:

baz = { foo: {} };
baz.foo.bar = Function;
bop = new baz.foo.bar();

console.log(bop);

And the slightly more complex case:

var baz = { foo: {} };
baz.foo.bar = function(){};
var bop = new baz.foo.bar();

console.log(bop);

But fails in the aforementioned case due to hoisting.

References

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.