JavaScript创建对象的方式:
实例
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <script> // 构造方法创建JavaScript对象 function Stu(naem,sex,age){ this.name = name; this.sex = sex; this.age = age; } let stu = new Stu('小白','男',20); console.log(stu); // 函数创建对象 function cer(name,age){ var obj = new Object(); obj.name = name; obj.age = age; return obj; } let cer1 = cer('天达',22); console.log(cer1); // 字面量创建对象 let arr = {name:'天弘',fun:function(){return this.name}} console.log(arr.name); </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例