08.对象补充知识(方法).html 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script>
  9. /*
  10. 创建一个对象
  11. */
  12. var obj = new Object();
  13. //向对象中添加属性
  14. obj.name = "孙悟空";
  15. obj.age = 18;
  16. //对象的属性值可以是任何的数据类型,也可以是一个函数
  17. obj.sayName = function(){
  18. console.log(obj.name);
  19. };
  20. function fun(){
  21. console.log(obj.name);
  22. };
  23. // console.log(obj.sayName);
  24. //调方法
  25. obj.sayName();
  26. //调函数
  27. // fun();
  28. /*
  29. 函数也可以成为对象的属性,
  30. 如果一个函数作为一个对象的属性保存,
  31. 那么我们称这个函数是这个对象的方法
  32. 调用这个函数就说调用对象的方法(method)
  33. 但是它只是名称上的区别,没有其他的区别
  34. */
  35. /*
  36. console.log();//调用console的log()方法
  37. document.write();//调用document的write()方法
  38. "hello".toString();//调用hello的toString()方法
  39. */
  40. var obj2={
  41. name:"二师兄",
  42. age:18,
  43. sayName:function(){
  44. console.log(obj2.name);
  45. }
  46. };
  47. obj2.sayName();
  48. </script>
  49. </head>
  50. <body>
  51. </body>
  52. </html>