How do I start my literal object as a function like you do when you use the new Object method.

e.g. var fn = function(){ ...code...}
var func = new fn();

I'm trying to achieve this using the literal object method like:
var fn ={
key: ''
}

Recommended Answers

All 2 Replies

Look up "javascript constructor" :D

<script>
function construct(key, value) {
this[key]=value;
}

var foo = new construct("foo",1);

alert(foo.foo);
</script>

But that isn't the literal way of doing it, is it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.