Table of contents
1. Execute the following program, the output result is ()
3. What kind of output will the following JS code do?
1. Output after inverting the integer parameter
1. Multiple choice questions
1. Execute the following program, the output result is ()
class Phone{
constructor(price){
this.price = price;
}
get price(){
return 999;
}
}
var p = new Phone(888);
console.log(p.price);
A、999
B、undefined
C. Throw an exception
D、888
Correct answer: C Your answer: A
Parse:
(1) When a property in a class has only the get() method but no set() method, the property cannot be assigned a value, even the initialization in the construction method is not possible. Therefore, when the price property of the object is set in the construction method initialization, an exception will be thrown
(2) The Phone class only sets the get method and does not set the set method, so the default price attribute is read-only, so you cannot set this.price = price in the constructor
①Solution 1: Do not set this.price = price in the constructor
<script>
class Phone {
constructor(price) {
// this.price = price;
}
get price() {
return 999;
}
}
var p = new Phone(888);
console.log(p.price);//输出:999
</script>
② Add a set method to the Phone class
<script>
class Phone {
constructor(price) {
this.price = price;
}
get price() {
return 999;
}
set price(item){
return item
}
}
var p = new Phone(888);
console.log(p.price);
</script>
(3) Class class document
4.3 ES6 Class | Novice Tutorial
2. How to get the text of the selected part of the select field in the form below, where obj=document.getElemById(“obj”)
<form name="a">
<select name="a" size="1" id=”obj”>
<option value="a">1</option>
<option value="b">2</option>
<option value="c">3</option>
</select>
</form>
A、obj.options[obj.selectedIndex].text
B、obj.options[obj.selectedIndex].value
C、obj. value
D、obj.text
Correct answer: A
Parse:
(1) The current default selection is the first one
- obj.options[obj.selectedIndex].text output text is 1
- obj.options[obj.selectedIndex].value output value is a
(2) Get the node object
<body>
<form name="a">
<select name="a" size="1" id="obj">
<option value="a">1</option>
<option value="b">2</option>
<option value="c">3</option>
</select>
</form>
<script>
window.onload = function () {
//获得下拉框的节点对象;
var obj = document.getElementById("obj");
console.log('obj',obj);
//1.获得当前选中的值:
var value = obj.value;
console.log('value',value);
//2.获得该下拉框所有的option的节点对象
var options = obj.options;
console.log('options',options);
//注意:得到的options是一个对象数组
//3.获得第0个option的value值:
var value1 = options[0].value;
console.log('value1',value1);
//4.获得第0个option的文本内容:
var text1 = options[0].text;
console.log('text1',text1);
//5.获得当前选中的option的索引
var index = obj.selectedIndex;
console.log('index',index);
//6.获得当前选中的option的文本内容
var selectedText = options[index].text;
console.log('selectedText',selectedText);
}
</script>
</body>
3. What kind of output will the following JS code do?
let a = 'w'
let obj = {
a: 'o',
print: function() {
console.log(this.a);
},
}
let p = obj.print;
obj.print();
p();
A、o、o
B、w、w
C、o、w
D、o、undefined
Correct answer: D Your answer: C
Parse:
(1) let a = 'w': Unlike the var keyword, the variable declared in the global scope using let will not become the property of the window object, because the variable declared by let will not be mounted on the window, so it is window The a variable below can only be undefined
(2) obj.print: this points to obj , so the output is the value of variable a in obj, namely o
(3) p(): Because the this point cannot be passed, the this of the function p points to the window
2. Programming questions
1. Output after inverting the integer parameter
Example: _reverse(0) -> 0 _reverse(233) -> 332 _reverse(-223) -> -322
Parse:
Use the array reverse() to reverse the method
<script>
let number = -223
function _reverse(number){
if(number >=0){
return Math.abs(number).toString().split("").reverse().join("") * 1
}else{
return Math.abs(number).toString().split("").reverse().join("") * (-1)
}
}
console.log(_reverse(number));
</script>
the code | explain |
---|---|
Math.abs(number) |
take the absolute value |
toString() |
number to string |
split("") |
String split to array |
reverse() |
array inversion |
join("") |
Merge array to string |