開発者がJavaScriptオブジェクトをコピーしました。 01 function Person() { 02 this.firstName = " John " ; 03 this.lastName = " Doe " ; 04 this.name = () = > `${this.firstName},${this.lastName}`; 05 } 06 07 const john = new Person(); 08 const dan = Object.assign({}, john); 09 dan.firstName = ' Dan ' ; 開発者はどのようにしてダンの名(firstName)と姓(lastName)にアクセスするのですか?
正解:B
* Person instances have: * firstName and lastName as string properties. * A name method that returns a combined string: `${this.firstName},${this.lastName}`. * Object.assign({}, john) creates a shallow copy of john into a new object, dan. After: dan.firstName = ' Dan ' ; * dan.name() returns " Dan,Doe " . Analysis of options: * A: dan.firstName() and dan.lastName() are function calls , but firstName/lastName are strings, not functions # TypeError. * B: Calls the defined method and uses both names correctly. * C: dan.name is a function reference; you'd still need to call it: dan.name(). * D: dan.firstName + dan.lastName is " DanDoe " , no separator. It accesses the properties but not in the method the developer defined. The intended way, using the provided API, is dan.name().