zoukankan      html  css  js  c++  java
  • hasOwnProperty 递归 简单回调 链式调用

    1、hasOwnProperty 函数的返回值为Boolean类型。如果对象object具有名称为propertyName的属性,则返回true,否则返回false。

    function Box(){
        this.a="123";
    }
    var box1=new Box();
    box1.hasOwnProperty("a");    //返回true

    如果在原型上添加一个属性,则返回false

    // 想要查看对象(包括原型链)是否具备指定的属性,可以使用in操作符

    function Box(){
        this.a="123";
    }
    var box1=new Box();
    Box.prototype.abc="345"
    
    box1.hasOwnProperty("abc");    //返回false;
    "abc" in box1   // 返回true

    2、复习递归

    function box(num){
        if(num<=1){
            return 1;
        }else{
            return num*box(num-1);
        }
    }
    box(3);
    
    通过arguments.callee来调用函数本身
    
    function box(num){
        if(num<=1){
            return 1;
        }else{
            return num*arguments.callee(num-1);
        }
    }
    box(3);

    3、简单的回调函数

    function $(id){
        return document.getElementById(id);
    }
    Object.prototype.show=function(fn){
        if(fn && fn.constructor==Function){   //判断,有fn和fn是一个函数
            fn();    //直接执行
        }else{
        this.style.display="none";}
    }
    $("c").onclick=function(){
        this.show(function(){alert()});
    }
    //把li节点隐藏,利用回调方法 
    var appendDiv=function( callback){
      var li=document.getElementsByTagName('li');
      for(var i=0;i<li.length;i++){
        if(typeof callback==='function'){
          callback(li[i])
        }
      }
     }
     appendDiv(function(node){
      node.style.display='none';
     })

    4、链式调用

    function $(id){
            return new _$(id);
        }
        function _$(id){
            this.elements = document.getElementById(id);
        }
        _$.prototype = {
            constructor:_$,
            hide:function(){
                console.log('hide');
                return this;
            },
            show:function(){
                console.log('show');
                return this;
            },
            getName:function(callback){
                if(callback){
                    callback.call(this,this.name);
                }
                return this;
            },
            setName:function(name){
                this.name = name;
                return this;
            }
        }
        $('c').setName('xesam').getName(function(name){
            console.log(name);
        }).show().hide().show().hide().show();
  • 相关阅读:
    关系数据理论
    JavaScript语言——对象
    网络编程基础入门级
    数据库加快查询速度索引
    C/C++随机函数的生成(转载)
    sql连接查询
    深入浅出HTTP请求
    17搜索如何抓全网页
    搜索引擎之百度一下
    搜索引擎之中搜
  • 原文地址:https://www.cnblogs.com/change-oneself/p/5379533.html
Copyright © 2011-2022 走看看