构造函数创建对象

时间:2021-02-22 12:12:49   收藏:0   阅读:0
//构造函数创建对象
function student(name,age){
  this.name = name;
  this.age = age;
}
function dog(name,age){
  this.name = name;
  this.age = age;
}
var stud1 = new student("狗蛋",18);
var dog1 = new dog("tom",1);
console.log("我叫"+stud1.name+"今年"+stud1.age+"岁了,平时很调皮,我朋友说我很狗,但是我不是真的狗")
console.log("我叫"+dog1.name+"今年"+dog1.age+"岁了,我是真的狗")
console.log(stud1 instanceof student);//学生是学生类,狗是狗类,但他们都是object类
console.log(dog1 instanceof dog);
console.log(stud1 instanceof dog);
console.log(dog1 instanceof student);
console.log(stud1 instanceof Object);
console.log(dog1 instanceof Object);

 

运行结果:

技术图片

 

 

//构造函数创建对象
function student(name,age){
  this.name = name;
  this.age = age;
}
function dog(name,age){
  this.name = name;
  this.age = age;
}
var stud1 = new student("狗蛋",18);
var dog1 = new dog("tom",1);
console.log("我叫"+stud1.name+"今年"+stud1.age+"岁了,平时很调皮,我朋友说我很狗,但是我不是做的狗")
console.log("我叫"+dog1.name+"今年"+dog1.age+"岁了,我是真的狗")
console.log(stud1 instanceof student);//学生是学生类,狗是狗类,但他们都是object类
console.log(dog1 instanceof dog);
console.log(stud1 instanceof dog);
console.log(dog1 instanceof student);
console.log(stud1 instanceof Object);
console.log(dog1 instanceof Object);
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!