How to overload constructor of an Object in JS (Javascript)?

前端 未结 6 646
小鲜肉
小鲜肉 2021-02-01 01:19

Can I do something like?:

function User(form) {
    this._username = form.username.value;
    this._password = form.password.value;
    this._surname = form.surn         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 01:42

    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) and createUserFromUserInfo(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.

提交回复
热议问题