上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function Person(name, age, height, judge){
this.name = name+"美女";
this.age = age+"岁";
this.height = height+"cm";
this.cooking = function(){
console.log("不会做饭")
}
let o = new Object();
o.name = name;
o.age = age;
o.height = height;
this.cooking = function(){
console.log("会做饭")
}
if(judge){
return o
}
}
let nonew = Person('小红',18,160,false);
let hnew = Person('小红',18,160,true);
let aa = new Person('小红',18,160,true);
let bb = new Person('小红',18,160,false);
</script>
</body>
</html>
运行结果如下:
举例:
function Person(name, age) {
var a = new Array();
a.name =name;
a.age=age;
a.sayName = function(){
console.log(this. name);
};
a. sayAge=function(){
console.log(this.age);
}
return a;
}
let cc = new Person("小红", 18);
let dd = new Person("小明", 18);
运行结果如下: