一、构造函数和原型
1. 对象的三种创建方式
-
字面量方式
var obj = {};
-
new关键字
var obj = new Object();
-
构造函数方式
function Person(uname,age){ this.uname = uname; this.age = age; } var obj = new Person('spring',21);
2. 构造函数
构造函数是一种特殊的函数,主要用来初始化对象,即为对象成员变量赋初始值,它总与new
一起使用。我们可以把对象中一些公共的属性和方法抽取出来,然后封装到这个函数里面。
使用构造函数时:
- 首字母要大写
- 和 new 一起使用
new 在执行时会做四件事情:
- 在内存中创建一个新的空对象。
- 让
this
指向这个新的对象。- 执行构造函数里面的代码,给这个新对象添加属性和方法。
- 返回这个新对象(所以构造函数里面不需要 return )。
3.静态成员和实例成员
-
实例成员:
-
在构造函数内部通过
this
创建的对象成员。eg :uname
age
sing
(都是通过this来添加的)function Star(uname,age) { this.uname = uname; this.age = age; this.sing = function(){ console.log('我会唱歌'); } } var six = new Star('spring',21);//实例化对象;
-
只能由实例化的对象来访问。eg: ‘ spring ’ 可以通过
six.uname
来访问,但是Star.uname
nonono
-
-
静态成员:
-
在构造函数本身上添加的成员
Star.sex='男';
-
只能由构造函数本身来访问。可以通过
Star.sex
来访问,但是six.sex
nonono
-
4. 构造函数原型对象 prototype
-
构造函数的问题
- 开辟多个内存空间
- 浪费内存
-
解决方法:prototype
每一个构造函数都有一个
prototype
属性,指向另一个对象。构造函数通过原型分配的函数是所有对象所共享的。
Star.prototype.sing = function() {
console.log('我会唱歌');
}
我们可以把那些不变的方法,直接定义在 prototype 对象上,这样所有对象的实例就可以共享这些方法。
5. 对象原型 __proto__
对象都会有一个属性__proto__
指向构造函数的 prototype
原型对象
__proto__
对象原型和原型对象prototype
是等价的
console.log(ldh.__proto__ === Star.prototype); //ture
// 方法的查找规则: 首先先看ldh对象身上是否有sing方法,如果有就执行这个对象上的sing
// 如果没有sing 这个方法,因为有__proto__ 的存在,就去构造函数原型对象prototype身上去查找sing这个方法
6.constructor 构造函数
对象原型( __proto__
)和构造函数(prototype)原型对象里面都有一个属性 constructor 属性 ,constructor 我们称 为构造函数,因为它指回构造函数本身。
// Star.prototype.sing = function() {
// console.log('我会唱歌');
// };
// Star.prototype.movie = function() {
// console.log('我会演电影');
// }
Star.prototype = {
// 这里是等于号,覆盖了原有的constructor对象,则必须手动指回原来的构造函数
constructor: Star,
sing: function() {
console.log('我会唱歌');
},
movie: function() {
console.log('我会演电影');
}
}
7. 构造函数、实例、原型对象三者之间的关系
8.原型链
9.JavaScript 的成员查找机制(规则)
- six.sex='男';
function Star(uname,age) {
this.uname = uname;
this.age = age;
}
var six = new Star('spring',21);
six.sex='男';
console.log(six.sex); //男
- Star.prototype.sex='男';
function Star(uname,age) {
this.uname = uname;
this.age = age;
}
Star.prototype.sing = function(){
console.log('我会唱歌');
}
Star.prototype.sex='男';
var six = new Star('spring',21);
console.log(six.sex); //男
- Object.prototype.sex = '男';
function Star(uname,age) {
this.uname = uname;
this.age = age;
}
Star.prototype.sing = function(){
console.log('我会唱歌');
}
Object.prototype.sex = '男';
var six = new Star('spring',21);
console.log(six.sex); //男
- 就近原则
function Star(uname,age) {
this.uname = uname;
this.age = age;
}
Star.prototype.sing = function(){
console.log('我会唱歌');
}
Star.prototype.sex='男';
var six = new Star('spring',21);
six.sex='女';
console.log(six.sex); //女
-
原型链
任何对象都有原型对象,也就是prototype属性,任何原型对象也是一个对象,该对象就有__proto__属性,这样一层一层往上找,就形成了一条链,我们称此为原型链;
当访问一个对象的属性(包括方法)时,首先查找这个对象自身有没有该属性。
如果没有就查找它的原型(也就是 __proto__指向的 prototype 原型对象)。
如果还没有就查找原型对象的原型(Object的原型对象)。
依此类推一直找到 Object 为止(null)。
__proto__对象原型的意义就在于为对象成员查找机制提供一个方向,或者说一条路线。
10. this指向
- 构造函数中的this 指向我们实例对象. eg:six
- 原型对象里面放的是方法, 这个方法里面的this 指向的是 这个方法的调用者, 也就是这个实例对象.
function Star(uname, age) {
this.uname = uname;
this.age = age;
}
var that;
Star.prototype.sing = function() {
console.log('我会唱歌');
that = this;
}
var six = new Star('spring', 21);
// 在构造函数中,里面this指向的是对象实例 six
six.sing();
console.log(that === six); //ture
11.扩展内置对象
原有内置对象
可以通过原型对象,对原来的内置对象进行扩展自定义的方法。
// 原型对象的应用 扩展内置对象方法
Array.prototype.sum = function() {
var sum = 0;
for (var i = 0; i < this.length; i++) {
sum += this[i];
}
return sum;
};
// Array.prototype = {
// sum: function() {
// var sum = 0;
// for (var i = 0; i < this.length; i++) {
// sum += this[i];
// }
// return sum;
// }
// }
var arr = [1, 2, 3];
console.log(arr.sum());
console.log(Array.prototype);
var arr1 = new Array(11, 22, 33);
console.log(arr1.sum());
注意:数组和字符串内置对象不能给原型对象覆盖操作 Array.prototype = {} ,只能是 Array.prototype.xxx = function(){} 的方式。
二、继承
通过构造函数+原型对象模拟实现继承,被称为组合继承。
1. call()
- 调用函数 + 修改函数运行时的 this 指向
fun.call(thisArg, arg1, arg2, ...)
- thisArg :当前调用函数 this 的指向对象
- arg1,arg2 :传递的其他参数
// call 方法
function fn(x, y) {
console.log('我想喝手磨咖啡');
console.log(this);
console.log(x + y);
}
var o = {
name: 'andy'
};
// fn();
// 1. call() 可以调用函数
// fn.call();
// 2. call() 可以改变这个函数的this指向 此时这个函数的this 就指向了o这个对象
fn.call(o, 1, 2);
2. 借用构造函数继承父类型
(1).属性
function Father(uname, age){
this.uname = uname;
this.age = age;
}
// 1
function Son(uname, age, score){
// 调用父构造函数 把父构造函数里面的this改成子构造函数的this
Father.call(this, uname, age);
this.score = score;
}
var six = new Son('春杨', 21, 100);
console.log(six);
//Son {uname: "春杨", age: 21}
//age: 21
//uname: "春杨"
//__proto__: Object
(2).方法
一般情况下,对象的方法都在构造函数的原型对象中设置,通过构造函数无法继承父类方法。
核心原理: ① 将子类所共享的方法提取出来,让子类的 prototype 原型对象 = new 父类()
② 本质:子类原型对象等于是实例化父类,因为父类实例化之后另外开辟空间,就不会影响原来父类原型对象 ③ 将子类的 constructor 从新指向子类的构造函数
function Father(uname, age){
this.uname = uname;
this.age = age;
}
Father.prototype.money = function(){
console.log(100000);
}
function Son(uname, age, score){
// 调用父构造函数 把父构造函数里面的this改成子构造函数的this
Father.call(this, uname, age);
this.score = score;
}
// Son.prototype = Father.prototype; 这样直接赋值会有问题,如果修改了子原型对象,父原型对象也会跟着一起变化
Son.prototype = new Father(); // 指向了Father的实例对象
// 这个是子构造函数专门的方法
// 如果利用对象的形式修改了原型对象,别忘了利用constructor 指回原来的构造函数
Son.prototype.constructor = Son;
Son.prototype.exam = function(){
console.log('孩子要考试');
}
var six = new Son('春杨', 21, 100);
console.log(six);
console.log(Father.prototype);
console.log(Son.prototype.constructor);
三、ES5新增方法
- 数组方法
- 字符串方法
- 对象方法
1. 数组方法
迭代(遍历)方法:forEach()、map()、filter()、some()、every();
(1). forEach()
array.forEach(function(currentValue, index, arr));
- currentValue:数组当前项的值
- index:数组当前项的索引
- arr:数组对象本身
var arr = [1,2,3];
var sum = 0;
arr.forEach(function(value, index, array){
console.log('每个数组元素' + value);
console.log('每个数组元素的索引号' + index);
console.log('数组本身' + array);
sum += value;
}
)
console.log(sum);
(2). filter() --> 筛选数组
array.filter(function(currentValue, index, arr))
- currentValue: 数组当前项的值
- index:数组当前项的索引
- arr:数组对象本身
// filter 筛选数组
var arr = [12, 66, 4, 88, 3, 7];
var newArr = arr.filter(function(value, index) {
// return value >= 20; //会返回一个新数组
return value % 2 === 0; // 求偶数
});
console.log(newArr);
(2). some() --> 查找数组中是否有满足条件的元素
array.some(function(currentValue, index, arr))
-
currentValue: 数组当前项的值
-
index:数组当前项的索引
-
arr:数组对象本身
注意:
-
返回值是布尔值, 如果查找到这个元素, 就返回
true
, 如果查找不到就返回false
. -
如果找到第一个满足条件的元素,则终止循环. 不在继续查找.
-
// some 查找数组中是否有满足条件的元素
// var arr = [10, 30, 4];
// var flag = arr.some(function(value) {
// // return value >= 20;
// return value < 3;
// });
// console.log(flag);
var arr1 = ['red', 'pink', 'blue'];
var flag1 = arr1.some(function(value) {
return value == 'pink';
});
console.log(flag1);
-
filter 查找满足条件的元素 返回的是一个数组 把所有满足条件的元素返回回来
-
some 查找满足条件的元素是否存在 返回的是一个布尔值 如果查找到第一个满足条件的元素就终止循环
(4). trim() --> 删除一个字符串的两端空白字符
str.trim()
不影响原字符串本身,它返回的是一个新的字符串。
// trim 方法去除字符串两侧空格
var str = ' an dy ';
console.log(str); // an dy
var str1 = str.trim(); //an dy
console.log(str1);
var input = document.querySelector('input');
var btn = document.querySelector('button');
var div = document.querySelector('div');
btn.onclick = function() {
var str = input.value.trim();
if (str === '') {
alert('请输入内容');
} else {
console.log(str);
console.log(str.length);
div.innerHTML = str;
}
}
2. 对象方法
(1). Object.keys() --> 用于获取对象自身的所有属性
Object.keys(obj)
var obj = {
id: 1,
pname: '小米',
price: 1999,
num: 2000
};
var arr = Object.keys(obj);
console.log(arr); // (4) ["id", "pname", "price", "num"]
arr.forEach(function(value) {
console.log(value);
})
/* id
pname
price
num */
(2). Object.defineProperty() --> 定义新属性或修改原有的属性。
Object.defineProperty(obj, prop, descriptor);
obj:必需。目标对象 prop:必需。需定义或修改的属性的名字 descriptor:必需。目标属性所拥有的特性
- Object.defineProperty() 第三个参数 descriptor 说明:
- value: 设置属性的值
- writable: 值是否可以重写。
true | false
- enumerable: 目标属性是否可以被枚举。
true | false
<不允许遍历 存在这个值但是你是看不见的 !隐私!> - configurable: 目标属性是否可以被删除或是否可以再次修改特性
true | false
var obj = {
id: 1,
pname: '小米',
price: 1999,
};
Object.defineProperty(obj, 'num', {
value:1000
});
console.log(obj); // {id: 1, pname: "小米", price: 1999, num: 1000}
Object.defineProperty(obj, 'price', {
value:99 // {id: 1, pname: "小米", price: 99, num: 1000}
});
console.log(obj);
Object.defineProperty(obj, 'id', {
writable: false, // false 不允许修改属性值
});
obj.id = 2;
console.log(obj); // {id: 1, pname: "小米", price: 99, num: 1000}
Object.defineProperty(obj, 'address', {
value: '中国山东蓝翔技校xx单元',
// 存在这个值但是你是看不见的 隐私!
enumerable: false, // enumerable 如果值为false 则不允许遍历, 默认的值是 false
configurable: false // configurable 如果为false 则不允许删除这个属性 不允许在修改第三个参数里面的特性 默认为false
});