如何在 JS (Javascript) 中重载对象的构造函数?
我可以做类似的事情吗?:
function User(form) {
this._username = form.username.value;
this._password = form.password.value;
this._surname = form.surname.value;
this._lastname = form.lastname.value;
this._birthdate = form.b_day.value+"-"+form.b_month.value+"-"+form.b_year.value;
this._avatar = form.avatar;
this._messages = new Array();
this._messagesCount=0;
}
function User(userName,password,surname,lastName,birthdate) {
this._username = userName;
this._password = password;
this._surname = surname;
this._lastname = lastName;
this._birthdate = birthdate;
this._avatar = form.avatar;
this._messages = new Array();
this._messagesCount=0;
}
Can I do something like?:
function User(form) {
this._username = form.username.value;
this._password = form.password.value;
this._surname = form.surname.value;
this._lastname = form.lastname.value;
this._birthdate = form.b_day.value+"-"+form.b_month.value+"-"+form.b_year.value;
this._avatar = form.avatar;
this._messages = new Array();
this._messagesCount=0;
}
function User(userName,password,surname,lastName,birthdate) {
this._username = userName;
this._password = password;
this._surname = surname;
this._lastname = lastName;
this._birthdate = birthdate;
this._avatar = form.avatar;
this._messages = new Array();
this._messagesCount=0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你不能这样做,因为 JavaScript 不是强类型语言,它不会看到表单和用户名之间的差异。您可以创建多个函数,例如
createUserFromForm(form)
和createUserFromUserInfo(userName, password,...)
或者您可以尝试使用不指定参数的单一构造函数,然后使用参数集合来检查输入并决定要做什么。You can't do that, since JavaScript is not a strongly typed language it will not see a difference between form and userName. You can create multiple function like
createUserFromForm(form)
andcreateUserFromUserInfo(userName, password,...)
or you could try to use a singular constructor with no arguments specified and then use arguments collection to check the input and decide what to do.我喜欢 Ilya Volodins 的回答,我想我应该添加这个作为例子:
可能有更好的方法可以做到这一点,但这只是一个例子。
I like Ilya Volodins answer and I thought I would add this as an example:
There are probably nicer ways of doing this but this just one example.
通过计算参数数量来重载构造函数或任何其他 Javascript 函数:
您还可以通过检测缺少多少参数来设置默认参数。
Overload the constructor or any other Javascript function by counting the number of arguments:
You can also set default arguments by detecting how many arguments are missing.
不,你不能,JavaScript 不支持任何类型的重载。
您可以做的是将已经填充了值的对象传递到构造函数中,然后从该对象中获取值,但这会重复代码。
或者,您可以创建一个默认构造函数并添加诸如
initFromUser
或setFromForm
之类的方法,然后使用相应的参数并设置对象值,new User().initFormForm (form)
对我来说看起来很干净。No you can't, JavaScript does not support overloading of any kind.
What you can do is either pass an object which has already been populated with the values into your constructor and then grab the values from the object, but this which duplicates code.
Or you can create a default constructor and add methods such as
initFromUser
orsetFromForm
which then take the respective parameters and setup the objects values,new User().initFormForm(form)
looks pretty clean to me.您可以使用 ES6 功能创建构造函数,如下所示。
打印输出:
You can create constructor using ES6 features as below.
Print Out:
您可以使用 JSON 字符串和 typeof 命令的组合轻松模拟重载方法和构造函数。请参阅下面的示例 - val 属性根据传入的数据类型而形成:
You can easily simulate overloaded methods and constructors using a combination of JSON strings and the typeof command. See example below - you the val attribute gets shaped from the type of data coming in: