new是什么?
new运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。
例子
<!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 Bottle(name,price,isKeepWarm){
this.name=name;
this.price=price;
this.isKeepWarm=isKeepWarm;
}
var bottle = new Bottle("杯子",20,true);
</script>
</body>
</html>
new一下都经历了什么?
- 创建新对象
- 将function(构造函数)的this指向新对象并赋值
- 将新对象指向Bottle实例
怎么判断new出来实例和对象之间的关系呢?
关键词:instanceof