现实世界的对象由形态和行为组成,js中对应的是属性和函数。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>js object</title> </head> <body> <script> //①用json表示 var student = { "name": "xxx", "age": 24, "sayHi": function () { alert(‘Hi~~~‘); } }; alert(student.name); //访问对象属性 student.sayHi(); //调用对象函数 //②用函数表示 var Teacher = function (name, age) { this.name = name; this.age = age; this.sayHi = function () { alert("hi,i‘m misssWang"); } } var misssWang = new Teacher(‘wangMs‘, 30); alert(misssWang.name); //访问对象属性 misssWang.sayHi(); //调用对象函数 var now = new Date(); //Date 为js内置函数 alert(typeof now); //now 的类型 ? </script> </body> </html>
时间: 2024-12-28 21:40:49