如何判断是引用数据类型的哪一种
- typeof 只能检测基本数据类型,检测引用数据类型时,无论是哪一种都返回object
- 利用Array.isArray(arr)判断是数组还是对象
- arr.constructor == Array /arr.constructor == Object判断
- s1 instanceof Student /判断s1 是否是Student的实例化对象
类
- 类,又称为构造函数,构造方法,是具有相同属性和方法的一类集合
- 类名注意开头字母大写
- 属性在方法里
- 方法写在原型里
- 不建议将所有方法都卸载创建的类里面,为了便于修改最好写在原型(prototype)里面
- 原型里面的方法和属性,所有的实例化对象都可以共享
- 注意传参的时候如果有一个项没有传参,那么会返回undefined
- 传参时的属性可以通过this.name = name设置
代码
<body>
<script>
var arr = [1,2,3]
console.log(arr)
// 判断引用数据类型到底是数组还是对象的三种方法 Array.isArray ,arr.constructor == Array,instanceof
console.log(Array.isArray(arr))
console.log(arr.constructor == Array)
console.log(arr.constructor == Object)
function Student(name,age,weight){
this.name = name ,
this.age = age,
this.weight = age
// 注意这里是this.name = name
}
var s1 = new Student("cyy",18,110);
Student.prototype.eat = function(){
console.log("eating>>>>>>>")
}
console.log(s1)
console.log(s1 instanceof Student)
// s1是否是Student的实例化对象
var s2 = new Student('aa', 7);
console.log(s2)
console.log(s2.name)
</script>
</body>
</html>
结果