Fakt, coś tam próbowałeś, ale robisz to w tak dziwaczny sposób, że nie zauważyłem. Poczytaj jak działa this
-> http://bonsaiden.github.io/JavaScript-Garden/pl/#function.this
Nie używaj pseudotablicy arguments
, to bardzo zła praktyka - nazywaj swoje argumenty:
function Acid(first, rest){
var that = this;
this.hydrogen = null;
this.rest = null;
if (first && rest) {
specifedAcid(first, rest);
} else if (first) {
namedAcid(first);
}
function namedAcid(name){
that.hydrogen = name.substring(0, 2);
that.rest = name.substring(2);
}
function specifedAcid(hydrogen, rest){
that.hydrogen = hydrogen;
that.rest = rest;
}
}
Acid.prototype.printAcid = function(){
console.log("Hydrogen part: " + this.hydrogen);
console.log("Acid rest part: " + this.rest);
console.log("Full acid name: " + this.hydrogen + this.rest);
};
var h2so4 = new Acid("H2", "SO4");
var h2so3 = new Acid("H2SO3");
h2so4.printAcid();
h2so3.printAcid();
Ogólnie pewnie trochę inaczej bym to zrobił, ale trzymając się Twojej koncepcji powinno to być właśnie coś takiego.