1.1-面向对象三大特征介绍
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function Animal ( name ) {
this.name = name;
};
let cat = new Animal('猫咪');
cat.eat = function ( food ) {
console.log ( "喵喵猫" + "我吃了" + food );
};
let dog = new Animal('小狗狗');
dog.eat = function ( food ) {
console.log ( "汪汪汪" + "我吃了" + food );
};
function Person ( name ) {
this.name = name;
};
Person.prototype.work = function (animal,food ) {
animal.eat(food);
};
let p1 = new Person('ikun');
p1.work(cat, '饼干');
p1.work(dog, '翔');
</script>
</body>
</html>
1.2-替换原型继承
<!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>
let father = {
house : {
address:'武汉天地',
price : 10000000
},
car:{
brand:'劳斯莱斯幻影',
price:8000000
}
}
function Son(name,age){
this.name = name
this.age = age
}
Son.prototype.eat = function(){
console.log('吃东西')
}
Son.prototype = father
let son1 = new Son('爽哥',20)
let son2 = new Son('ikun',30)
console.log(son1,son2)
</script>
</body>
</html>
02-原型链
==1.1-原型链介绍==
- 1.原型链:每一个对象都有原型,原型本身又是对象,所以原型又有原型,以此类推形成一个链式结构,称为原型链
- 2.对象访问原型链中的成员规则:就近原则
- 当访问一个对象的成员变量时,会首先访问它自身的成员变量,如果有则访问。没有则在原型中寻找,能找到就访问,不能找到则继续往原型的原型中寻找,以此类推,如果找到原型链的顶端还是找不到,则程序报错:
xxx is not a function

<!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 Person(name,age){
this.name = name;
this.age = age;
};
Person.prototype.sayHi = function(){
console.log('人生若只如初见,何事秋风悲画扇');
};
Person.prototype.type = '哺乳动物';
let p1 = new Person('又又',18);
console.log(p1);
console.log(p1.name);
console.log(p1.type);
console.log(p1.hobby);
p1.sayHi();
p1.toString();
console.log(p1.__proto__.constructor);
console.log(p1.__proto__ === Person.prototype);
console.log(p1.__proto__.__proto__.constructor);
console.log(p1.__proto__.__proto__ === Object.prototype);
console.log(p1.__proto__.__proto__.__proto__);
</script>
</body>
</html>
1.Array的原型链
let arr = new Array(10,20,30);
console.log ( arr );
console.log ( arr.__proto__.constructor );
console.log ( arr.__proto__ === Array.prototype );
console.log ( arr.__proto__.__proto__.constructor );
console.log ( arr.__proto__.__proto__ === Object.prototype );

2-Date的原型链
let date1 = new Date();
console.dir(date1);
console.log ( date1.__proto__ === Date.prototype );
console.log ( date1.__proto__.__proto__.constructor );
console.log ( date1.__proto__.__proto__ === Object.prototype );

3-String对象原型链
let str = new String('123');
console.log ( str );
console.log ( str.__proto__ === String.prototype );
console.log ( str.__proto__.__proto__.constructor );
console.log ( str.__proto__.__proto__ === Object.prototype );

4-DOM对象原型链
let div1 = document.getElementById('div1');
let p1 = document.getElementById('p1');


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="div1"></div>
<p id="p1"></p>
<script>
let arr = new Array(10,20,30);
console.log ( arr );
console.log ( arr.__proto__.constructor );
console.log ( arr.__proto__ === Array.prototype );
console.log ( arr.__proto__.__proto__.constructor );
console.log ( arr.__proto__.__proto__ === Object.prototype );
let date1 = new Date();
console.dir(date1);
console.log ( date1.__proto__ === Date.prototype );
console.log ( date1.__proto__.__proto__.constructor );
console.log ( date1.__proto__.__proto__ === Object.prototype );
let str = new String('123');
console.log ( str );
console.log ( str.__proto__ === String.prototype );
console.log ( str.__proto__.__proto__.constructor );
console.log ( str.__proto__.__proto__ === Object.prototype );
let div1 = document.getElementById('div1');
let p1 = document.getElementById('p1');
</script>
</body>
</html>
1.3-instanceof运算符
let arr = [10,20,30];
console.log ( arr instanceof Array );
console.log ( arr instanceof Object );
console.log ( Function instanceof Function );
console.log ( Function instanceof Object );
console.log ( Object instanceof Object );
console.log ( Object instanceof Function );
03-ES6类与继承
1.1-class关键字介绍

<!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>
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
};
sayHi() {
console.log('你好,我是黑马李宗盛');
};
eat() {
console.log('大吉大利今晚吃鸡');
};
};
Person.prototype.type = '人类';
let p1 = new Person('ikun', 30);
console.log(p1);
</script>
</body>
</html>
1.2-extends关键字介绍

<!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>
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
};
sayHi() {
console.log('你好,我是黑马李宗盛');
};
eat() {
console.log('大吉大利今晚吃鸡');
};
};
Person.prototype.type = '人类';
let p1 = new Person('ikun', 30);
console.log(p1);
class Student extends Person{
learn(){
console.log('今天学的很开心');
};
};
let s1 = new Student('班长',20);
console.log(s1);
s1.learn();
s1.eat();
</script>
</body>
</html>
1.3-super关键字介绍
<!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>
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
};
sayHi() {
console.log('你好,我是黑马李宗盛');
};
eat() {
console.log('大吉大利今晚吃鸡');
};
};
Person.prototype.type = '人类';
let p1 = new Person('ikun', 30);
console.log(p1);
class Student extends Person{
constructor(name,age,score){
super(name,age);
this.score = score;
}
learn(){
console.log('1.我要吃饭了');
super.eat();
console.log('3.我开始学习了');
};
};
let s1 = new Student('班长',20,99);
console.log(s1);
s1.learn();
s1.eat();
</script>
</body>
</html>
1.1-arguments关键字
<!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 fn(){
console.log(arguments)
}
fn(10,20,30,40)
</script>
</body>
</html>
1.2-剩余参数(rest参数)
<!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 fn(a,b,...c){
console.log(arguments)
console.log(a,b)
console.log(c)
}
fn(10,20,30,40)
</script>
</body>
</html>
1.3-函数默认参数
<!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 fn(a, b = 20) {
console.log(a)
console.log(b)
console.log(a + b)
}
fn(10,5)
fn(10)
</script>
</body>
</html>