<script>function person(firstname,lastname,age,eyecolor){this.firstname=firstname;this.lastname=lastname;this.age=age;this.eyecolor=eyecolor;this.changeName=changeName;function changeName(name){this.lastname=name;}}myMother=new person("Steve","Jobs",56,"green");myMother.changeName("Ballmer");document.write(myMother.lastname);</script>這段代碼中。this.firstname=firstname;前面的 firstname 和后面的 firstname 分別代碼什么。上面function person() 括號里面的幾個,又是this 中前面的,還是后面的。有什么關系?還有就是this.firstname=firstname;這個前面和后面的 firstname 必須寫的一樣么?分別是代表什么含義。
2 回答

瀟瀟雨雨
TA貢獻1833條經驗 獲得超4個贊
這里的person相當于一個類,其他的編程語言用class聲明,javascript里用function聲明。
由于js是若類型編程語言,在person函數內部,this.firstname相當于創建了一個類屬性,在person的參數列表中,傳入的是形參。給你個例子吧。
function person(firstname, lastname){ this .firstName = firstname; this .lastName = lastname; } var a = new persion( "this is firstName" , "this is lastName" ); console.log(a.firstName); // this is firstName console.log(a.lastName); //this is lastName |
添加回答
舉報
0/150
提交
取消