Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Forked from dnoiz1/pilot.js
Created February 18, 2012 05:45
Show Gist options
  • Save deoxxa/1857667 to your computer and use it in GitHub Desktop.
Save deoxxa/1857667 to your computer and use it in GitHub Desktop.
pilot object
var Pilot = function(data) {
this.charID = 0;
this.charName = null;
this.solarSystemID = null;
this.solarSystemName = null;
this.shipTypeID = null;
this.shipTypeName = null;
this.shipID = null;
this.shipName = null;
this.stationID = null;
this.stationName = null;
this.corpID = null;
this.corpName = null;
this.allianceID = null;
this.allianceName = null;
if (typeof data === "object") {
this.setAll(data);
}
};
Pilot.prototype.setAll = function(data) {
this.charID = data.charID;
this.charName = data.charName;
this.solarSystemID = data.solarSystemID;
this.solarSystemName = data.solarSystemName;
this.shipTypeID = data.shipTypeID;
this.shipTypeName = data.shitTypeName;
this.shipID = data.shipID;
this.shipName = data.shipName;
this.stationID = data.stationID;
this.stationName = data.stationName;
this.corpID = data.corpID;
this.corpName = data.corpName;
this.allianceID = data.allianceID;
this.allianceName = data.allianceName;
console.log(this); // <--- Pilot
return this;
};
var p = new Pilot();
magic_ajax_thing_i_suggest_reqwest({
url: "/some/thing.json",
type: "json",
success: function(err, data) {
p.setAll(data);
console.log(p);
},
});
console.log(p);
// basically all that matters is how setAll is called
//
// if you pass it on its own (i.e. {success: p.setAll})
// it'll be called on its own, with `this' being set to the
// default global object. in a browser, this is `window'.
// if you pass it in wrapped in a function that calls it as
// the member of an object (i.e. as above), it'll have
// `this' set to the object it's called from. here's an
// example:
function LOL() {
this.herp = "derp";
}
LOL.prototype.rage = function() {
console.log(this);
};
var lol = new LOL();
var a = function() { lol.rage(); }
var b = lol.rage;
a(); // will dump the `lol' object
b(); // will dump the `window' object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment