以下のコードを参照してください。 01 let o = { 02 get js() { 03 let city1 = String( ' St. Louis ' ); 04 let city2 = String( ' New York ' ); 05 06 return { 07 firstCity: city1.toLowerCase(), 08 secondCity: city2.toLowerCase(), 09 } 10 } 11 } 開発者は、o.js.secondCityを参照することでどのような価値を期待できるのでしょうか?
正解:D
1. Getter Functions in JavaScript In JavaScript, when an object uses the get keyword, it defines a getter method . Accessing a getter property executes the function and returns its value. Thus: o.js does not return the getter function; instead, it executes the function located at: get js() { ... } and returns the object inside the return block. 2. Behavior of String() and toLowerCase() Inside the getter: let city1 = String( ' St. Louis ' ); let city2 = String( ' New York ' ); String() creates a string value. Then, the returned object is constructed as: { firstCity: city1.toLowerCase(), secondCity: city2.toLowerCase(), } The method toLowerCase() is a standard JavaScript string method that returns a new string with all alphabetic characters converted to lowercase . Therefore: city2.toLowerCase() returns: ' new york ' 3. Referencing the Property When the developer writes: o.js.secondCity the following happens: * The getter js runs and returns an object. * The returned object includes the property: secondCity: ' new york ' * Accessing .secondCity retrieves the lowercase string ' new york ' . Therefore, the correct value is ' new york ' . Why the Other Options Are Incorrect A). undefined - incorrect because the property secondCity clearly exists in the returned object. B). An error - incorrect because no invalid operations occur; all methods and properties are valid. C). ' New York ' - incorrect because toLowerCase() transforms the string to lowercase. JavaScript Knowledge References (Text-Based) * Getter methods using the get keyword return computed values when accessed. * JavaScript String values support the toLowerCase() method, which returns a lowercase version of the original string. * Accessing nested properties like o.js.secondCity triggers the getter, returning the constructed object.