面向对象-2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /**
         * 1. 函数数据类型
         *    普通函数
         *    箭头函数
         *    生成函数
         *    构造函数(类)
         * 
         *   对象数据类型
         *    普通对象/ 数组对象 / 正则对象 / 日期对象 ...
         *    实例也是对象类型(排除7种原始类型值)
         *     prototype/__proto__原型属性值也是对象(排除Function.prototype) 

            2 大部分函数(重点是构造函数)都内置一个 prototype(原型【显示原型】)的属性,属性值是一个对象,
              对象中存储的属性和方法,是供当前类所属实例,调用的“公共”的属性和方法
               + 箭头函数是没有 prototype的属性的
               + 在原型对象上有一个内置的属性 constructor(构造器),属性值是当前函数本身

         *   3 每一个对象都内置一个 __proto__(原型链【银饰原型】)属性,属性值都指向自己所属类的原型prototype对象
                + Object.prototype这个对象的 __proto__ 值是null,因为Object是所有对象的“基类”
        */


        // let arr = [10,20,30];

        // console.log(Array.prototype);
        // console.log(Object.prototype);
        // console.log(arr);

        // function Fn(){
        //     this.x = 100; 
        //     this.y = 200;
        //     this.getX = function(){
        //         console.log(this.x);
        //     }
        //  } 
        // Fn.prototype.getX = function(){  console.log(this.x); } 
        // Fn.prototype.getY = function(){ console.log(this.y); }  
        // let f1 = new Fn ;   let f2 = new Fn;
        // console.log(f1.getX === f2.getX);  //   false  [都是私有的方法]
        // console.log(f1.getY === f2.getY);  //   true   [都是公共的方法]
        // console.log(f1.__proto__.getY === Fn.prototype.getY);  //  true
        // console.log(f1.__proto__.getX === f2.getX);    // false
        // console.log(f1.getX === Fn.prototype.getX); // false
        // console.log(f1.constructor);  // Fn
        // console.log(Fn.prototype.__proto__.constructor); // Object() { [native code] }
        // f1.getX();  // 100
        // f1.__proto__.getX();  // undefined
        // f2.getY(); // 200
        // Fn.prototype.getY();  // undefined


        // 实现内置new方法
        // function Dog(name){
        //     this.name = name;
        // }

        // Dog.prototype.bark = function(){
        //     console.log('wangwang');
        // }

        // Dog.prototype.SayName = function(){
        //     console.log('my name is ' + this.name);
        // }


        // let sanmao = new Dog('三毛')
        // sanmao.SayName();
        // sanmao.bark()

        // 1
        // function _new(ctor,...params){
        //     // 1 创建一个实例对象
        //     let obj = Object.create(ctor.prototype); 
        //     // 2 会把构造函数当做是普通的函数执行
        //     // this指向创建的实例
        //     let result = ctor.call(obj,...params);
        //     // 观察函数执行的返回值 
        //     if(/^(object|function)$/.test(typeof result )) return result; 
        //     return obj;
        // }
 

        // 2

        // 重写的方法只考虑 pro 传递的是一个对象
        // Object.create = function(pro){
        //     function Proxy(){} 
        //     Proxy.prototype = pro
        //     return new Proxy;
        // }

        // function _new(ctor){
        //     // 获取除第一个参数以外,剩余传递的参数信息,以数组的形式保存到params中
        //     var params = [].slice.call(arguments,1)
        //     // 1 创建一个实例对象 
        //     // Object.create 兼容ie 低版本浏览器 需要改写
        //     var  obj = Object.create(ctor.prototype); 
        //     // 2 会把构造函数当做是普通的函数执行
        //     // this指向创建的实例 
        //     // 基于 apply 既可以改变this,也可以把数组中的每一项传递给函数
        //     var result = ctor.apply(obj,params);
        //     // 观察函数执行的返回值 
        //     if(/^(object|function)$/.test(typeof result )) return result; 
        //     return obj;
        // }


        // var sanmao = new _new(Dog,'三毛')
        // sanmao.SayName(); // my name is 三毛
        // sanmao.bark() // wangwang 
        // console.log(sanmao instanceof Dog); // true



        // Object.create(pro) 创建一个空对象,把pro作为当前创建空对象的 __proto__的指向
        // + pro  可以传递 null 或者一个对象

        // let pro = {
        //     A:10,
        //     B:20
        // } 
        // // console.log(Object.create()); 
        // console.log(Object.create(pro));


// -----------------------------------------------
      /** 
       *  扩展内置类型上的方法  
      */

    //  Array.prototype.unique = function unique(){ 
    //     let result = new Set(this)
    //     result = Array.from(result)
    //     return result
    //   }

    //   // 先去重 再排序
    //   let arr = [1,2,6,5,3,2,3,4,2,3,4,2,1,2,3,4,5,3,4];
 
    //   let result = arr.unique().sort((a,b)=> a - b )     
    //   console.log(result);


    // 练习题 1
    // function fun(){
    //     // 实例的私有属性和方法
    //     this.a = 0 ;
    //     this.b = function(){
    //         alert(this.a)
    //     }
    // }  

    // // 实例的公有属性和方法
    // fun.prototype = {
    //     b:function(){
    //         this.a = 20;
    //         alert(this.a);
    //     },
    //     c:function(){
    //         this.a = 30;
    //         alert(this.a)
    //     }
    // }

    // var my_fun = new fun();
    // my_fun.b();  // 0
    // my_fun.c();  // 30 


    // 练习题 2 
    // function C1(name){
    //     if(name){
    //         this.name = name;
    //     }
    // }

    // function C2(name){ 
    //     this.name = name; 
    // }

    // function C3(name){ 
    //     this.name = name || 'join'; 
    // }

    // C1.prototype.name = 'Tom';
    // C2.prototype.name = 'Tom';
    // C3.prototype.name = 'Tom';
    //         // 'Tom          undefined         join'
    // alert( (new C1().name) + (new C2().name) + (new C3().name))  


     // 练习题 3 
    //  function Fn(){
    //      let a = 1;
    //      this.a = a;
    //  }   

    //  Fn.prototype.say = function(){
    //      this.a = 2;
    //  }

    //  Fn.prototype = new Fn;
    //  let f1 = new Fn ;
    //  Fn.prototype.b = function(){
    //      this.a = 3;
    //  }
 
    //  console.log(f1.a); // 1
    //  console.log(f1.prototype); // undefined
    //  console.log(f1.b); //  ƒ (){ this.a = 3; }
    //  console.log(f1.hasOwnProperty('b')); // false
    //  console.log('b' in f1); // true
    //  console.log(f1.constructor == Fn); // true


    // 练习题 4 
    // function Foo(){
    //     getName = function(){  console.log(1); } 
    //     return this;
    // } 
    // Foo.getName = function(){   console.log(2);   } 
 
    // Foo.prototype.getName = function(){  console.log(3);  } 
    // var getName = function(){  console.log(4);  } 
    // function getName(){   console.log(5);   } 

    // Foo.getName();  // 2
    // getName();  // 4
    // Foo().getName(); // 1
    // getName();  // 1
    // new Foo.getName();  // 2
    // new Foo().getName();  // 3
    // new new Foo().getName(); // 3


    // 练习题  5 
    // const check = value => {
    //     value = +value;
    //     return isNaN(value) ?  0 : value;
    // }
    // Number.prototype.plus = function(value){ 
    //     return this + check(value) 
    // } 
    // Number.prototype.minus = function(value){
    //     return this - check(value)
    // } 
    // let n = 10;
    // let m = n.plus(10).minus(5);
    // console.log(m); // 10 + 10 -5
 
    </script> 
</body>
</html>
posted @ 2021-07-20 16:03  13522679763-任国强  阅读(21)  评论(0)    收藏  举报