构造函数创建对象的原因:解决一个一个构造对象的麻烦,简化构造对象的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> function Star (name, age, sex) { this.name=name; this.age= age; this.sex = sex; } var ldh = new Star('刘德华',18,'男'); var zxy = new Star('张学友',18,'男'); console.log(ldh.name); console.log(zxy.name); </script> </head> <body> </body> </html>
注意:不需要return,函数名第一个字母要大写,