Javascript 重载构造函数
有了AngularJS中的以下“类”,我似乎无法重载构造函数方法,我需要这样做,这样我还可以使用API中的数据来构造它 我该怎么做才能正确地做到这一点 字符类 Angular的$injector负责创建/实例化您的服务和工厂,因此您不能直接要求构造函数参数。但是,如果您有一个需要自定义输入的输入,您可以通过以下几种方式进行处理: 创建一个公开实例化类的工厂方法的服务,例如CharacterBuilder.createparams。 向服务中添加一个init方法,该方法通常充当构造函数,例如Character.initparams。Javascript 重载构造函数,javascript,angularjs,class,constructor,overloading,Javascript,Angularjs,Class,Constructor,Overloading,有了AngularJS中的以下“类”,我似乎无法重载构造函数方法,我需要这样做,这样我还可以使用API中的数据来构造它 我该怎么做才能正确地做到这一点 字符类 Angular的$injector负责创建/实例化您的服务和工厂,因此您不能直接要求构造函数参数。但是,如果您有一个需要自定义输入的输入,您可以通过以下几种方式进行处理: 创建一个公开实例化类的工厂方法的服务,例如CharacterBuilder.createparams。 向服务中添加一个init方法,该方法通常充当构造函数,例如Cha
也许你可以指向一行,或者添加注释,或者缩小代码的范围,或者我们应该阅读所有内容吗?请告诉我们你希望如何调用构造函数,以便我们告诉你如何重载它。字符类做得太多了。它包括控制器和数据服务,以及模型。您需要将它们分为各自的类:CharacterController、CharacterService和Character。@GregBurghardt谢谢您的建议,我会试试的。我对AngularJS甚至javascript都很陌生,所以每一条建议都非常受欢迎:-
angular.module('sheetApp')
.factory('Character', function ($rootScope, $resource, $state, ApiLocation) {
function Character() {
this.name = "";
this.level = 1;
this.health = 1;
this.mana = 1;
this.race = null;
this.guild = null;
this.colors = [];
this.attributes = {Strength: 1, Intelligence: 1, Dexterity: 1, Endurance: 1, Charisma: 1};
this.passives = [];
this.skills = [];
this.spells = [];
this.equipment = [];
this.consumables = [];
this.user = "";
this.maxPassives = 4;
this.maxSpells = 4;
angular.forEach($rootScope.skills, function (skill) {
this.push({skill: skill, points: [false, false, false, false, false], level: 0});
},
this.skills);
}
Character.prototype = {
setRace: function (race) {
this.race = race;
$rootScope.notification = {
type: "success",
text: "Added race " + race.name + " to character.",
show: true,
timer: 5000
};
$state.go('sheet');
},
setGuild: function (guild) {
this.guild = guild;
$rootScope.notification = {
type: "success",
text: "Added guild " + guild.name + " to character.",
show: true,
timer: 5000
};
$state.go('sheet');
},
// Add passive to character, checks against duplicates.
addPassive: function (passive) {
var duplicate = false;
angular.forEach(this.passives, function (pas) {
if (passive.name === pas.name) {
duplicate = true;
$rootScope.notification = {
type: "danger",
text: "Character already has " + passive.name,
show: true,
timer: 5000
};
}
});
if (!duplicate) {
if (this.passives.length < this.maxPassives) {
this.passives.push(passive);
$rootScope.notification = {
type: "success",
text: "Added passive " + passive.name + " to character.",
show: true,
timer: 5000
};
$state.go('sheet');
}
else {
$rootScope.notification = {
type: "danger",
text: "You already have " + this.maxPassives + " passives",
show: true,
timer: 5000
};
$state.go('sheet');
}
}
},
// Add spell to character, checks against duplicates.
addSpell: function (spell) {
var duplicate = false;
angular.forEach(this.spells, function (sp) {
if (spell.name === sp.name) {
duplicate = true;
$rootScope.notification = {
type: "danger",
text: "Character already has " + spell.name,
show: true,
timer: 5000
};
}
});
if (!duplicate) {
this.spells.push(spell);
$rootScope.notification = {
type: "success",
text: "Added spell " + spell.name + " to character.",
show: true,
timer: 5000
};
$state.go('sheet');
}
},
resetSkillPoints: function () {
angular.forEach(this.skills, function (skill) {
for (var i = 0; i <= 4; i++) {
if (skill.points[i] == true) {
skill.level += 1;
}
skill.points[i] = false;
}
});
}
};
Character.resource =
$resource(ApiLocation + '/characters/:link', {}, {
getUser: {method: 'GET', params: {link: 'user', name: '@name'}, isArray: true},
getChar: {method: 'GET', params: {link: 'character', name: '@name'}},
save: {method: 'POST'},
query: {method: 'GET', isArray: true, cache: true},
remove: {method: 'DELETE'},
delete: {method: 'DELETE'}
});
return ( Character );
});