高级js 面向对象 和面向过程 三种函数
判断数据类型
// 创建一个Cat对象,属性:颜色,品种,行为:吃,跑,捉老鼠
var Cat = new Object() //new一个对象
Cat.catys = 'red' //属性
Cat.catname = 'cat' //对象名
// 行为
Cat.catxw = function () {
console.log("喜欢跑,吃,捉老鼠");
}
Cat.catxw()
console.log(Cat.catys, Cat.catname)
工厂函数 构造函数
// //工厂函数
// function create(catys, catname) {
// return {
// sid: catys,
// sname: catname,
// xw: function () {
// console.log("1000" + catys + catname)
// }
// }
// }
// var stu1 = create("1003", "cat")
// stu1.xw()
// 第三种构造函数
function Student(stuid, stuname) {
this.stu = stuid //当前的stuid
this.stuname = stuname //当前的stuname
this.study = function () {
console.log("我爱学习" + stuid)
}
}
var stu_1 = new Student("10001", "张三")
console.log(stu_1.stuid) //10001
// stu_1.study() //调用函数里面的study
// console.log(stu_1 instanceof Student); //true
// console.log(stu_1.constructor === Student)
undefined与null的区别?: undefined代表定义未赋值nulll定义并赋值了,只是值为null
什么时候给变量赋值为null呢?初始赋值,表明将要赋值为对象
结束前,让对象成为垃圾对象(被垃圾回收器回收)
严格区别变量类型与数据类型?
数据的类型
基本类型对象类型
变量的类型(变量内存值的类型)
基本类型:保存就是基本类型的数据
引用类型:保存的是地址值
面向过程:
一步一步来 ,
打开冰箱,大象进去,关上冰箱
面向对象:
万物皆对象
如 :
大象关进冰箱。 大象是一个对象让大想进去
冰箱是一个对象:让冰箱打开冰箱,和关闭
<script>
//第一种使用字面量的方式 new Object()
var student = new Object()
student.stuid = '1001' //属性
student.stuName = '张三' //属性
student.study = function () { // 动作
console.log("我爱学习")
}
student.study()
console.log(student.stuid, student.stuName)
// 创建一个 stu_1对象
var stu_1 = {
stuid: '1002',
stuName: "张一",
eat: function () {
console.log("跑步");
}
}
</script>