1.原型

1.1 prototype

原型prototype其实是function对象的一个属性,而prototype本身也是对象。

  1. function Handphone(){}
  2. console.log(Handphone.prototype);

image.png

  1. 这个prototype是定义构造函数出来的每个对象的公共祖先。
  2. 所有被该构造函数构造出来的对象都可以继承原型上的属性和方法。
  1. // prototype 实际上是一个function 的属性
  2. function Handphone(color, brand){
  3. this.color = color;
  4. this.brand = brand;
  5. }
  6. Handphone.prototype.rom = '64G';
  7. Handphone.prototype.ram = '6G';
  8. var phone1 = new Handphone('red', 'xiaomi');
  9. var phone2 = new Handphone('black', 'iphone');
  10. // console.log(Handphone.prototype);
  11. console.log(phone1);
  12. console.log(phone2);
  13. console.log(phone1.rom);
  14. console.log(phone2.ram);
  15. 1. 这个prototype是定义构造函数出来的每个对象的公共祖先。
  16. 2. 所有被该构造函数构造出来的对象都可以继承原型上的属性和方法。
  17. function Handphone(color, brand){
  18. this.color = color;
  19. this.brand = brand;
  20. }
  21. Handphone.prototype.rom = '64G';
  22. Handphone.prototype.ram = '6G';
  23. Handphone.prototype.screen = '18:9';
  24. Handphone.prototype.system = 'Android';
  25. Handphone.prototype.call = function(){
  26. console.log('I am calling somebody');
  27. }
  28. var hp1 = new Handphone('red', 'xiaomi');
  29. var hp2 = new Handphone('Black', 'huawei');
  30. hp1.call();
  31. hp2.call();

公共部分的属性,方法,都可以写在prototype上来继承

  1. function Handphone(color, brand){
  2. this.color = color;
  3. this.brand = brand;
  4. }
  5. Handphone.prototype = {
  6. rom: '64G',
  7. ram: '6G',
  8. screen: '18:9',
  9. system: 'Android',
  10. call: function(){
  11. console.log('I am calling somebody');
  12. }
  13. }
  14. var hp1 = new Handphone('red', 'xiaomi');
  15. var hp2 = new Handphone('Black', 'huawei');
  16. hp1.call();
  17. hp2.call();

1.2 constructor

constructor => 构造函数本身

  1. constructor是原型里的一个属性,它指向的是构造函数本身,因此所有被该构造函数实例化出来的对象,都可以继承原型上的方法和属性.
  2. constructor的指向可以被人修改 ```javascript function Handphone(color, brand, system){ this.color = color; this.brand = brand; this.system = system; }

function Telphone(){

}

Handphone.prototype = { constructor: Telphone }

var hp1 = new Handphone(‘red’, ‘xiaomi’, ‘Android’); console.log(hp1);

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/26116833/1645613808868-1380d102-5d0c-401d-a72f-8b9b30974230.png#clientId=u2c39abd6-3a0c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=151&id=u60310d3f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=189&originWidth=1249&originalType=binary&ratio=1&rotation=0&showTitle=false&size=27052&status=done&style=none&taskId=u97c0f7c0-4be3-46b0-a54a-031790240aa&title=&width=999.2)
  2. 原型属于实例化对象,他不属于构造函数.在实例化之前prototype没有挂靠对象,所以原型挂靠构造函数. 而在实例化对象之后,原型挂靠实例化出来的对象
  3. ```javascript
  4. function Car(){
  5. // __proto__: 每个实例化对象原型的容器
  6. // var this = { // 当构造函数被new实例化的时候
  7. // __proto__: Car.prototype
  8. // }
  9. }
  10. Car.prototype.name = 'Mazda';
  11. var car = new Car();
  12. // 1. new 首先创建一个全新的对象
  13. // 2. 这个新对象会被执行[[prototype]] 连接
  14. // 3. 这个新对象会被绑定到函数调用的this
  15. // 4. 如果函数不返回新对象, 那么new 表达式中的函数调用会自动返回这个新对象.
  16. console.log(car.name); // Mazda
  17. console.log(car); // Car{}

1.3proto也可以被更改

  1. function Person(){}
  2. Person.prototype.name = '张三';
  3. // Person.prototype ={
  4. // }
  5. var p1 = {
  6. name: '李四'
  7. }
  8. var person = new Person();
  9. console.log(person.name); // 张三
  10. person.__proto__ = p1;
  11. console.log(person.name); // 李四

在实例化之后给prototype重新赋值,只会保存在实例化对象下面的proto里面的constructor属性里面的proto中,只有重新实例化这个构造函数的时候,
constructor里面保存的值,才会被实例化出来.
因此在实例化之后给prototype重新赋值修改的是实例化之前constructor里面的值,当重新实例化这个构造函数的时候,constructor里面被重新赋的值就可以被访问.

  1. function Car(){}
  2. Car.prototype.name = 'Mazda';
  3. var car = new Car();
  4. Car.prototype.name = 'Benz';
  5. console.log(car.name);// 'Benz'
  1. function Car(){}
  2. Car.prototype.name = 'Benz';
  3. var car = new Car();
  4. Car.prototype = {
  5. name: 'Mazda';
  6. }
  7. console.log(car.name);// 'Benz'
  8. // 实例化时的操作
  9. Car.prototype.constructor => Car()
  10. Car.prototype => {name: Benz}
  11. function Car(){
  12. var this = {
  13. __proto__: Car.prototype = {
  14. name: 'Benz'
  15. }
  16. }
  17. }

image.png

2.闭包立即执行函数

2.1 return 和 window的区别

  • return

    1. function test(){
    2. var a = 1;
    3. function plus1(){
    4. a++;
    5. console.log(a);
    6. }
    7. return plus1;
    8. }
    9. // 必须把值返回出去,赋值给别的变量
    10. var plus = test();
    11. plus(); // 2
    12. plus(); // 3
  • window

    1. function test(){
    2. var a = 1;
    3. function add(){
    4. a++;
    5. console.log(a);
    6. }
    7. window.add = add;
    8. }
    9. test();
    10. add();

    在使用立即执行函数的时候,用return返回值的时候,需要声明一个全局变量保存函数的返回值.
    在使用立即执行函数的时候,用window可以把函数的返回值直接存储在window全局对象下. ```javascript var res = (function(){ var a = 1; function add(){

    1. a++;

    console.log(a); } return add; })() res(); // 2 res(); // 3 res(); // 4

(function test(){ var a = 1; function add(){ a++; console.log(a); } window.add = add; })()

add();// 2 add();// 3

  1. <a name="olUNH"></a>
  2. # 3. JS插件
  3. ```javascript
  4. ;(function(){
  5. function Test() {}
  6. Test.prototype = {
  7. }
  8. window.Test = Test;
  9. })()
  10. var test = new Test();

4. 作业

任意传俩个数字,调用插件内部方法可进行加减乘除功能

  1. // 方法1
  2. ;(function(){
  3. function Compute(){}
  4. Computer.prototype = {
  5. plus: function(a, b){
  6. return a + b;
  7. },
  8. min: function(a, b){
  9. return a - b;
  10. },
  11. mul: function(a, b){
  12. return a * b;
  13. },
  14. div: function(a, b){
  15. return a / b;
  16. }
  17. }
  18. window.Compute = Compute;
  19. })()
  20. var com = new Compute();
  21. // 方法2
  22. ;(function(){
  23. function Compute(opt){
  24. this.num1 = opt.num1;
  25. this.num2 = opt.num2;
  26. }
  27. Compute.prototype = {
  28. plus: function(){
  29. return this.num1 + this.num2;
  30. },
  31. min: function(){
  32. return this.num1 - this.num2;
  33. },
  34. mul: function(){
  35. return this.num1 * this.num2;
  36. },
  37. div: function(){
  38. return this.num1 / this.num2;
  39. }
  40. }
  41. window.Compute = Compute
  42. })()
  43. var com = new Compute({
  44. num1: 1,
  45. num2: 3
  46. });
  47. var res = com.mul();
  48. console.log(res);