Say I have a pure constructor function (containing nothing but this.Bar = bar)
1) When I call it from another function, can I pass the caller function’s arguments directly when I call or must I do var myBar=new bar, myBar.Bar=thebar, where the bar is a caller argument?
2) Will the constructor still instantiate even if it doesn’t get all the args?
3) How can I check if one of the args is unique, IE no other instance of the object has this value for the property in question? Specifically, I want to assign each object a unique index at creation. Maybe array?
Many thanks in advance
I’m going to assume you mean:
(Note: The overwhelming convention in JavaScript is that property names start with a lower-case letter. So
this.bar, notthis.Bar. Initially-capped identifiers are usually reserved for constructor functions.)You can pass them directly:
The number of arguments passed is not checked by the JavaScript engine. Any formal arguments that you don’t pass will have the value
undefinedwhen the function is called:In general, that’s usually not in-scope for a constructor. But yes, you could use an array, or an object, to do that.
Of course,
indexOfmay not be what you want for searching, so you may need to use some other method ofArray.prototypeor your own loop.Another way would be to use an object; this assumes that
baris a string or something that can usefully be turned into a string: