js函数

作者: 常连海 | 来源:发表于2017-07-13 13:48 被阅读0次

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    函数 < Function >
    JS是一门函数式的面向对象编程语言

          1.函数内的arguments设计缺陷,是个类数组,不能使用数组的方法, 解决办法--->>rguments.__proto__ = Array.prototype;
    
        2. 函数里面的this,缺陷,  var that = $(this); 保存变量
    
       3. for in 在便利对象的时候,会便利出原型上自己定义的方法和属性, 解决办法
        for (var key in obj) {
            if (obj.hasOwnProperty(key){}
        }
    

    4.没有块级作用域 if while do for (只有函数作用域和全局作用域)

    <script type='text/javascript'>
        //每个函数创建完毕有两个隐藏属性 __proto__ 、 prototype(里面有一个constructor属性,指向函数本身)
        //每个函数内部有俩个隐藏属性     this(执行主体) 、 arguments(函数内接收实参对象)<类数组(arry-like)> arguments.__proto__ = Array.prototype;
        //函数三角色: 对象、普通函数、类
    
        var fn = function() {};
        fn.__proto__ === Function.prototype;
        fn.prototype.constructor === fn;
        fn.__proto__.__proto__ === Object.prototype;
        fn instanceof Function;
    
        var obj = {};
        obj.__proto__ === Object.prototype;
    
        var ary = [];
        ary.__proto__.__proto__ === Object.prototype;
    
        Function.prototype instanceof Object;
        Object instanceof Function
    
        fn.__proto__ instanceof Object;
        fn.__proto__.__proto__ instanceof Object;  //false
    
    
        var Que = function(type) {
            this.type = type;
        };
        Que.prototype.get_status = function() {
            console.log(this.type);
        };
        Que.prototype.add = function() {
            console.log(arguments);
            console.log(this);
        }
        var myQue = new Que(1);
    
         myQue.get_status();
    
         //Apply
         var ary = [1, 2, 3];
         var sum = myQue.add.apply(null, ary);
    
         var obj1 = {
            type: 2,
         };
    
        var s = myQue.get_status.apply(obj1);
        console.log(s)
    
    QQ截图20170713135027.png
    </script>
    

    </body>
    </html>

    相关文章

      网友评论

          本文标题:js函数

          本文链接:https://www.haomeiwen.com/subject/yepwhxtx.html