I am trying to create a namespace that encapsulate all our javascript classes, however am getting a missing : after property id in ie9 and firefox on the POI.prototype.getFullAddress line (the stop after the POI). The second problem is when i try to initialize the object var x = new Makani.POI(a,b,c...) i get javascript errors and have to use var x = Makani.POI(a,b,c...) without the new keyword which is weird. I posted the full code for reference. Thanks for your help.
/* Makani Javascript Libraries */
var Makani = function () {
var private_var;
function private_method() {
// do stuff here
}
return {
POI: function(ID, Title, Section, District, City, Governorate) {
this.ID = ID;
this.Title = Title;
this.Section = Section;
this.City = City;
this.Goverorate = Governorate;
},
POI.prototype.getFullAddress = function (opts) {
if (("includeCity" in opts) && (opts["includeCity"] == false))
return this.Section + ", " + this.District;
else if (("includeGovernorate" in opts) && (opts["includeGovernorate"] == false))
return this.Section + ", " + this.District + ", " + this.City;
else return this.Section + ", " + this.District + ", " + this.City + ", " + 开发者_StackOverflowthis.Governorate;
}
};
} ();
An object initialiser (object literal) in JS looks like this:
{
propName: 123,
123: 'some value',
hmmAFunction: function(){}
}
Generally, it goes PropertyName : AssignmentExpression
with each key/value pair separated by a comma. So, when you do this:
return {
POI: function(ID, Title, Section, District, City, Governorate) {
},
// THIS
POI.prototype.getFullAddress = function (opts) {
}
};
... it's not valid, and will result in errors being thrown.
Try this instead:
var Makani = {
POI: (function(){
function POI (ID, Title, Section, District, City, Governorate) {
this.ID = ID;
this.Title = Title;
this.Section = Section;
this.City = City;
this.Goverorate = Governorate;
}
POI.prototype.getFullAddress = function (opts) {
if (("includeCity" in opts) && (opts["includeCity"] == false))
return this.Section + ", " + this.District;
else if (("includeGovernorate" in opts) && (opts["includeGovernorate"] == false))
return this.Section + ", " + this.District + ", " + this.City;
else return this.Section + ", " + this.District + ", " + this.City + ", " + this.Governorate;
};
return POI;
}())
};
Also, I advise against starting a variable name with a capital letter (e.g. District
) unless what it references is a constructor function (like POI
).
Probably you just want this:
POI.getFullAddress: function (opts) {
It looks like you're trying to change the prototype, but you're creating an object definition in that particular block. The other way would be to create the return
part as a var, then extend the prototype, then return it.
What you are doing at a simple level is this:
x = {
name1: value,
name2 = value
}
which isn't valid syntax.
精彩评论