1

I have my custom class and I want to store it into map collection, but while I want to get one of the value from the map (one instance of my object) I got error that object is undefined. Is there any possibility to cast object or something? If I push simple string instead of object of my custom class everything is ok. Someone can tell me how to solve this problem? Of course if this problem could be solved.

js:

function CustomClass(first, second, third){
    this.one = first;
    this.two = second;
    this.three = third;
}

CustomClass.prototype.constains(obj1, obj2){}

...
var dictionary = new Object();
dictionary[key] = CustomClass(1, "lel", 2);
for(var el in dictionary) //here IE debugger shows that I have a right key, but the value is undefined.
{
    if(dictionary[el].contains(something))
    {...}
}

If it depends I use IE10. Thanks for help and also for tips!

edit:

If I use

dictionary[key] = new CustomClass(1, "lel", 2);

instead of:

dictionary[key] = CustomClass(1, "lel", 2);

it works, but someone could tell me why, something like this:

dictionary[key] = "lel"; 

works without new operator? From what I know everything in js is an object so what's the difference beetwen inserting a string and a custom class? (I'm new in js, so sorry for my bad knowledge)

5
  • 1
    Your syntax is not right, you didn't use new to call CustomClass, can you post a demo to reproduce the issue? Try jsfiddle.net Commented Aug 5, 2014 at 7:42
  • 1
    var elem[key] = doesn’t look right Commented Aug 5, 2014 at 7:46
  • @elclanrs wow, Thank You for a very fast answer, if I add a new it works :) var elem[key] = new CustomClass(1, "lel", 2); instead of var elem[key] = CustomClass(1, "lel", 2); But can You tell me why, something like this: var elem[key] = "lel"; works without new operator? From what I know everything in js is an object so what's the difference beetwen inserting a string and a custom class? (I'm new in js, so sorry for my bad knowledge) Commented Aug 5, 2014 at 7:46
  • what is elem? You haven't declared that. Also, as David says, you shouldn't use var in front of elem[key] = Commented Aug 5, 2014 at 8:05
  • @Alnitak sorry I didn't noticed that I fix that. It should be dictionary[key] = .. Commented Aug 5, 2014 at 8:08

1 Answer 1

1

As you have already learned, in JS objects are constructed with functions, but the new keyword is critical.

When you do var foo = new Bar() the interpreter not only creates a new object and sets it to this within that constructor, that object is also then the implicit return value of the constructor.

If you omit new then within Bar the this context will will be the default (undefined in ES5 strict mode, or the global object - typically window otherwise), and the non-existent return value of the function (i.e. undefined) will be assigned to foo.

It is possible to turn a plain function into an "automatic constructor" that doesn't neeed the new keyword like this:

function Bar(x, y, z) {
    if (!(this instanceof Bar)) {
        return new Bar(x, y, z);
    }

    ...
}

i.e. the function can figure out that it wasn't invoked with new, so immediately just calls itself with new instead.

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.