因为前两种创建方法一次只能创建一个对象
一次创建一个对象,里面大量属性和方法是相同的
因此可以利用函数 的方法,重复这些相同的代码,就把这个函数成为构造函数
又因为这个函数,里面封装的不是普通代码,而是对象
构造函数就是把对象里一些相同的代码抽象出来封装到函数里
是一种特殊的函数,主要用来初始化对象,即因为对象成员变量赋值初始,他总与new运算符一起使用,我们可以吧对象中的一些公共属性和方法抽取出来,然后封装到这个函数里面
function 构造函数名(){
this.属性 = 值;//当前的属性
this.方法 = function(){//当前的方法
}
}
new 构造函数名();//调用
1.构造函数名的首字母要大写
2.构造函数和之前的普通函数比起来,属性和方法前面必须要加this,应为是批量创建,所以,调用的是谁,this指向的就是谁
3.传参和普通函数一样,也是实参形参,且一一对应
4.实参和属性不需要同名
5.调用方法是前面要加new:new Star();
,这个和函数的调用区别就是要多一个new
6.构造函数里不需要return就可以返回值
7.输出对象还是像以前一样,将其赋值给一个变量后输出
8.调用函数就等于创建了一个对象
function Star(uname, age, sex) {
this.starname = uname;
this.starage = age;
this.starsex = sex;
}
//console.log(new Star('刘德华', 18, '男'));用这种方法也可以输出,但是对象因为是一对多的关系,所以俺需要给他赋值一个变量名,就生成了一个新的对象
var ldh = new Star('刘德华', 18, '男');
console.log(ldh);
console.log(typeof ldh);
//如果要输出的是对象的一个属性
console.log(ldh.starname);
console.log(ldh['starsex']);
![](https://img.haomeiwen.com/i10629340/74a22d6003d1c8e7.png)
这样写的好处是可以批量创建对象:
function Star(uname, age, sex) {
this.starname = uname;
this.starage = age;
this.starsex = sex;
}
//console.log(new Star('刘德华', 18, '男'));
var ldh = new Star('刘德华', 18, '男');
console.log(ldh);
console.log(typeof ldh);
//如果要输出的是对象的一个属性
console.log(ldh.starname);
console.log(ldh['starsex']);
var zxy = new Star('张学友', 19, '男');
console.log(zxy);
console.log(zxy.starname);
console.log(zxy['starsex']);
![](https://img.haomeiwen.com/i10629340/2a2daf27b9f0c945.png)
谁调用函数,this就是指向谁
function Star(uname, age, sex) {
this.starname = uname;
this.starage = age;
this.starsex = sex;
this.singer = function(song) {
console.log(song); //这个song就是下面的冰雨
}
}
//输出一个对象的方法
var ldh = new Star('刘德华', 18, '男');
console.log(ldh.starname);
console.log(ldh.starage);
ldh.singer('冰雨');
var zxy = new Star('张学友', 19, '男');
console.log(zxy.starname);
console.log(zxy.starage);
zxy.singer('李香兰');
![](https://img.haomeiwen.com/i10629340/dc391daaa6069a43.png)
构造函数:如Star();抽象了对象的公共部分,封装到函数里,它
某一大类(class,js中没有类的概念)
创建对象:如 new Star();,某一个,通过new关键字创建对象的过程我们也叫对象实例化
通过泛指的构造函数用对象的实例化成为特指的某一个
1.当构造函数遇到new之后,首先会在内存中创建一个空对象
2.构造函数中的this会指向这个空对象
3.将形参传递给构造函数
4.执行构造函数中的代码,把里面的属性和方法执行完毕之后,给空对象添加属性和方法(把执行出来的结果传递给这个空对象)
5.返回这个对象,实例化的时候写的new就有return的作用,所以多想不需要写return
function Person(){
}
var per = Person();
console.log(per
此时per没有值,所以返回值是undefined:
![](https://img.haomeiwen.com/i10629340/af7aeaab4877a9c0.png)
function Person(){
}
var per = new Person();
console.log(per);
返回值:
![](https://img.haomeiwen.com/i10629340/dd182664b1208d23.png)
这个函数本身是没有内容的,但是他会有返回值,如果在函数里写一个 alert(this);的话:
function Person(){
alert(this);
}
var per = new Person();
console.log(per);
这个this就是新建的这个per
返回值:
![](https://img.haomeiwen.com/i10629340/078829dc5947e314.png)
![](https://img.haomeiwen.com/i10629340/2bc7229f1b8629e5.png)
//这个this就是per
function Person(){
this.name = "孙悟空";//向刚创建的构造函数里添加一个属性孙悟空用this
this.age = 18;
this.gender = "男";
this.sayName = function(){
alert(this.name);
}
}
var per = new Person();
console.log(per);
返回值是:
![](https://img.haomeiwen.com/i10629340/f5f4ce0e3e4f2743.png)
function Person(name,age,gender){//传参,以防参数写死
this.name = name;//用this向刚创建的构造函数里添加一个属性
this.age = age;
this.gender = gender;
this.sayName = function(){
alert(this.name);
}
}
var per = new Person("孙悟空",18,"男");
var per2 = new Person("猪八戒",18,"男");
var per3 = new Person("沙和尚",18,"男");
console.log(per);
console.log(per2);
console.log(per3);
返回值:
![](https://img.haomeiwen.com/i10629340/eedc73541ee9f364.png)
function Person(name,age,gender){
this.name = name;
this.age = age;
this.gender = gender;
this.sayName = function(){
alert(this.name);
}
}
function Dog(){
}
var per = new Person("孙悟空",18,"男");
var Dog = new Dog("猪八戒",18,"男");
console.log(per);
console.log(Dog);
返回值:
![](https://img.haomeiwen.com/i10629340/73bb26d07dec121f.png)
检查实例:
function Person(name,age,gender){
this.name = name;
this.age = age;
this.gender = gender;
this.sayName = function(){
alert(this.name);
}
}
function Dog(){
}
var per = new Person("孙悟空",18,"男");
var Dog = new Dog("猪八戒",18,"男");
//使用instanceof可以检查一个对象是否是一个类的实例
//检查per是否是person的实例,是返回true,不是返回false
console.log(per instanceof Person);
返回值:
![](https://img.haomeiwen.com/i10629340/2737fcf232f3a593.png)
function Person(name,age,gender){
this.name = name;
this.age = age;
this.gender = gender;
this.sayName = function(){
alert(this.name);
}
}
function Dog(){
}
var per = new Person("孙悟空",18,"男");
var Dog = new Dog("猪八戒",18,"男");
//使用instanceof可以检查一个对象是否是一个类的实例
//所有的对象都是Object的后代
//所以任何对象和Object做instanceof检查时都会返回true
console.log(per instanceof Object);
console.log(Dog instanceof Object);
![](https://img.haomeiwen.com/i10629340/40850cbaa0bb53a6.png)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
/*
* 创建一个构造函数,专门用来创建Person对象的
* 构造函数就是一个普通的函数,创建方式和普通函数没有区别,
* 不同的是构造函数 习惯上 首字母大写
*
* 构造函数和普通函数的区别就是调用方式的不同
* 普通函数是直接调用[ var per = Person(); ],而构造函数需要使用new关键字来调用[ var per = new Person(); ]
*
* 构造函数的执行流程:
* 1.立刻创建一个新的对象
* 2.将新建的对象设置为函数中this(这个this就是这个新建的对象),所以在构造函数中可以使用this引用新建的这个对象
* 3.逐行执行函数中的代码
* 4.将新建的对象作为返回值返回
*
*
*
* 使用同一个构造函数创建的对象,我们称为一类对象,也将一个构造函数称为一个类。
* 我们将通过一个构造函数创建的对象,称为是该类的实例
*
* this的情况:
* 1.当以函数的形式调用时,this是window
* 2.当以方法的形式调用时,谁调用方法this就是谁
* 3.当以构造函数的形式调用时,this就是新创建的那个对象
*
*/
function Person(name , age , gender){
this.name = name;
this.age = age;
this.gender = gender;
this.sayName = function(){
alert(this.name);
};
}
function Dog(){
}
var per = new Person("孙悟空",18,"男");
var per2 = new Person("玉兔精",16,"女");
var per3 = new Person("奔波霸",38,"男");
var dog = new Dog();
/*console.log(per);
console.log(dog);*/
/*
* 使用instanceof可以检查一个对象是否是一个类的实例
* 语法:
* 对象 instanceof 构造函数
* 如果是,则返回true,否则返回false
*/
//console.log(per instanceof Person);
//console.log(dog instanceof Person);
/*
* 所有的对象都是Object的后代,
* 所以任何对象和Object左instanceof检查时都会返回true
*/
//console.log(dog instanceof Object);
</script>
</head>
<body>
</body>
</html>
网友评论