function bark() {
console.log("Woof!");
}
bark.animal = "dog";
-
A: Nothing, this is totally fine!
-
B:
SyntaxError
. You cannot add properties to a function this way. -
C:
undefined
-
D:
ReferenceError
这在JavaScript
中是可能的,因为函数也是对象!(原始类型之外的所有东西都是对象)
函数是一种特殊类型的对象。您自己编写的代码并不是实际的函数。 该函数是具有属性的对象,此属性是可调用的。
答案: A
练习:
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title></title> 9 </head> 10 11 <body> 12 </body> 13 14 </html> 15 <script type="text/javascript"> 16 function bark() { 17 console.log("Woof!"); 18 } 19 20 bark.animal = "dog"; 21 console.log(bark.animal) 22 console.log(bark()) 23 </script>