码迷,mamicode.com
首页 > 其他好文 > 详细

构造函数创建对象

时间:2021-02-22 12:12:49      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:lin   function   new   height   com   mil   tps   style   朋友   

//构造函数创建对象
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);

构造函数创建对象

标签:lin   function   new   height   com   mil   tps   style   朋友   

原文地址:https://www.cnblogs.com/hmy-666/p/14423772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!