• 真我,无我


    心语——

      我们都认为再也不能做从前的我们,可是,就算世界把我们变成自己讨厌的人,我还是要寻找真正的自己。找回可以让我顺畅呼吸的一片天地,在此之前,无论付出多少!对了,我定是付出的不够!

    记录着几天的javascript 方面的收获:

    1. typeof 跟 instanceof 的区别?

      typeof 是运算符,返回的是字符串,表示的是运算的类型,有

      A: "undefined":  typeof undefined;

                变量声明未定义返回如 var a; typeof a; 

                未定义的变量

      B: "boolean":  typeof true;

      C: "number"

      D: "string":     typeof "abc"; 

      E: "function":    typeof Function;

                typeof Object;

      F: "object":     typeof null;

                typeof [1,2];

      instance表示实例。A instanceof B; 表示A是B的一个实例。这个 A 只能是一个对象才有可能返回 true。

      如:

        "abc" instanceof String;  //false

        var a = new String("abc");

        a instanceof String;    //true

        所有的变量 instanceof Object 都是true<前提这个变量是引用型,基本数据类型(Null, Undefined, Number , boolean ,String ), 其中

    Number , boolean ,String 也可以有自己的属性和方法也可是引用类型。>

    2. 原生js 实现点击显示隐藏?

      这道题目可以考察一下知识点:

        一:js 如何删除子节点,添加文本节点,找到孩子节点——DOM 元素操作

        二:if(){}else{}语句不能用在外层,因为如果条件满足,就执行only one time ! 在下面可以 把 onclick 函数写到if 语句里面试试,就知道了哈~

    .wrap a{color: #000;text-decoration: none;}
    .wrap a:hover{text-decoration: none;}
    .wrap .content{background: #999;width: 400px;height: 200px;}
    <div class="wrap">
        <a href="javascript:;" id="btn">点击显示</a>
        <div class="content" id="content" style="display:none;"></div>
    </div>
    <script type="text/javascript">
        var btn = document.getElementById("btn");
        var content = document.getElementById("content");
        
        //显示div
        function showDiv(){
            var txt = btn.childNodes;
            var showTxt = document.createTextNode("点击隐藏");
            btn.removeChild(txt[txt.length-1]);//这里注意,容易漏写 txt.length-1 因为这里应该是删除节点
            btn.appendChild(showTxt);
            content.style.display = "block";
        }
    
        //隐藏div
        function hideDiv(){
            var txt = btn.childNodes;
            var hideTxt = document.createTextNode("点击显示");
            btn.removeChild(txt[txt.length-1]);
            btn.appendChild(hideTxt);
            content.style.display = "none";
        }
    
        btn.onclick = function(){
            if(content.style.display == "none"){
                showDiv();
            }else{
                hideDiv();
            }
        }
        </script>

    睡觉前面记录下看的一道面试题:

    /**

     * 找出下面代码的问题

     */

    var Obj=function(msg){
      this.msg = msg;
      this.shout=function(){
        alert(this.msg);
      }
      this.waitAndShout=function(){
        setTimeout(this.shout, 2000);
      }
    }
    var aa = new Obj("abc");
    aa.waitAndShout();

      运行结果是undefined; 一看这个结果就知道是this 出问题了,那么问题是什么呢? 是闭包? 作用域? 查找下《javascript高级程序设计》,是匿名函数的this 是指向window 全局对象的, 而在全局对象中,并未定义 msg...不信在外面加 var msg ="abc";就弹出 "abc" 了。
      或者向下面一样(结果运行正确,有待补充,先睡觉了):

      var Obj=function(msg){
          this.msg = msg;
            _this = this;
          this.shout=(function(){
            alert(_this.msg);
          })();
          this.waitAndShout=function(){
            setTimeout(this.shout, 2000);
          }
        }
        var aa = new Obj("abc");
        aa.waitAndShout();

     上面代码等同于

      var Obj=function(msg){
          this.msg = msg;
            _this = this;
          this.waitAndShout=function(){
            setTimeout(function(){
                      alert(_this.msg);
                  }, 2000);
          }
        }
        var aa = new Obj("abc");
        aa.waitAndShout();
    疯癫不成狂,有酒勿可尝;世间良辰美,终成水墨白。
  • 相关阅读:
    TWaver3D直线、曲线、曲面的绘制
    react开发教程(六)React与DOM
    java Web开发实现手机拍照上传到服务器
    手把手教你从零写一个简单的 VUE--模板篇
    file_get_contents函数获取不到数据的一种情况
    Windows系统下批处理快速创建WIFI
    操作系统题库之简答题部分(个人整理,附带答案)
    回溯到底是个什么东西(含有大量经典例题加详细分析)
    到底什么是dp思想(内含大量经典例题,附带详细解析)
    一次性弄懂到底什么叫做分治思想(含有大量经典例题,附带详细解析)
  • 原文地址:https://www.cnblogs.com/chuyu/p/3155003.html
Copyright © 2020-2023  润新知