JavaScript OOP 创建对象
对象:
- 属性-------对象的某些特定的性质。
- 方法-------对象能做的事情。
- 事件-------能响应发生在对象上的事情。
构造型函数方式
- 构造函数就是初始化一个实例对象;
- 默认函数首字母大写;
- 当调用构造函数时,new会自动创建this对象,且类型就是构造函数类型;
对象字面量形式
- JavaScript还支持对象和数组字面量,允许使用一种简洁而可读的记法来创建数组和对象;
实例方法
- 利用javascript对象原型引用prototype来实现的;
静态方法
- 静态方法不能被实例对象调用;
- 只能通过函数来调用
成员的访问
- 使用“ . ”来访问对象属性;
- 使用“ [ ] ”来访问对象属性;
js的2大特性
- 动态性;
- 弱类型;
instanceof
- instanceof 就是判断一个实例是否属于某种类型。
构造器(Constructor)
- 函数构造器用于创建一个函数对象,在JavaScript中实际上每一个函数都是一个函数对象。
- 所有对象都会从它的原型上继承一个
constructor;
constructor
是原型对象上的一个属性,默认指向这个原型的构造函数。
new的简单理解
- 创建一个新对象;
- 将构造函数的作用域赋给新对象(因此 this 就指向了这个新对象) ;
- 执行构造函数中的代码(为这个新对象添加属性) ;
- 返回新对象。