zoukankan      html  css  js  c++  java
  • JS 零散知识点

    1.简单的clone方法

     MySQLStore.prototype.clone = function(object) {
       return JSON.parse(JSON.stringify(object));
    }

    另外其他clone,如:

      args = args ? args.slice(0) : []; // Clone array to avoid changing the original
        options = Object.assign({}, options); // Clone object to avoid changing the original

    2.在函数中给对象赋值

    function A(){
    }
    var data = {};
    A(data={
        x:"y"
    });
    //y
    console.log(data.x);

    3.对象和数组的区别之一

    function A(){
      var option ={
          a:"b"
      }; //x=undefined
      var option = ['a']; //x=1
      console.log("x="+option.length);
    }
    A();  //数组有长度,对象没有。

    4.判断语句中0和{}的地位

    if(0){
      console.log("hello");
    }else{
      console.log("kk");
    }
    //kk
    var x = {};
    console.log({}?"xx":"yy");
    //xx
    //0为false,"0"为true,[],{}为true

    5.void的作用

    function B(){
        console.log("B");
        return "C";
    }
    function A(){
        return void B();
    }
    
    var x = A();
    console.log("x=",x); //undefined
    //如果去掉void,则x=C;

     6.诡异代码

    function A(){
        function b(){
            return 'cc'
        }
        return {
            a:b(),
            b:9
        }
    }
    var options = A(); //b()已经执行
    console.log(options);
    //{ a: 'cc', b: 9 }

    诡异之处在与执行A(),b()也执行了,如果是b,就不执行。

    7. 引用赋值

    var x = {};
    var y = x;
    y.x1 = "y1";
    console.log("x=",x); 
    //x= { x1: 'y1' }

    如果这样:

    var x = {};
    var y = x;
    y = {        // 重新赋值了
        "x1":"y1"
    };
    console.log("x=",x);
    //x= {}

    8.引用

    var vm  = {};
    var vnode = {
        x:"y"
    }; 
    var prevVnode = vm._vnode;   
    //preVnode为脚本类型,指定即不能变化
    console.log("before prevVnode is:",prevVnode);   //undefined
    console.log("before vm._vnode is:",vm._vnode);  //undefined
    vm._vnode = vnode;  // 重新赋值了
    console.log("after prevVnode is:",prevVnode);   //undefined
    console.log("after vm._vnode is:",vm._vnode);   //{x:'y'}

     9.isObject和isPlainObject的区别

    var _toString = Object.prototype.toString;
    function isObject (obj) {
      return obj !== null && typeof obj === 'object'
    }
    function isPlainObject (obj) {
      return _toString.call(obj) === '[object Object]'
    }
    console.log(isPlainObject({}));
    console.log(isObject([]));

    10.小套路

    return typeof def === 'function' && getType(prop.type) !== 'Function'
        ? def.call(vm)   //把def函数中的this绑定到vm上,返回def函数中
                              //return的内容
        : def

     在vue中,Vue实例的选项很多可以是function或者object,对应function中可能会引用实例中的数据,那么在赋值的时候需要兼容下:

    typeof childVal === 'function' ? childVal.call(this, this) : childVal
    //或者
    childVal.call(vm, vm)

    11.decodeURIComponent

    可以用多次,有时候也必须用多次。

    12.实例获取到对象的方法

    // 两层意思,1 个Vue.挂个静态成员FunctionalRenderContext
    function Vue () {
    
    }
    Vue.options = {}
    Vue.options = {
        'components': {
          name: 'keep-alive',
          abstact: true
        }
    }
    var vue = new Vue();
    console.log(vue.constructor)
    // { [Function: Vue]
    // options: { components: { name: 'keep-alive', abstact: true } } }
    console.log(vue.constructor.options)
    //{ components: { name: 'keep-alive', abstact: true } }

    13.数组也是引用类型

    var arr = [1, 3, 4, 5]
    var kk = arr
    kk.splice(0, 2)
    console.log('arr:', arr)  //[4,5]
    //如何解决数组的硬拷贝了,有两种方法
    1.Object.assign()
    2.操作符(...)

      let a =[1,3,5];
      let b = Object.assign([], a);
      b.push('7');
      console.log('b=', b) // [1,3,5,'7']
      console.log('a=', a) // [1, 3, 5]

     

    又如:

    function processRawAttrs (el) {
      const l = el.attrsList.length
      if (l) {
        const attrs = el.attrs = new Array(l)
        for (let i = 0; i < l; i++) {
          attrs[i] = {
            name: el.attrsList[i].name,
            value: JSON.stringify(el.attrsList[i].value)
          }
        }
      } else if (!el.pre) {
        // non root node in pre blocks with no attributes
        el.plain = true
      }
    }
    var el = {
      attrsList:[{
        name: 'name',
        value: 'value'
      }]
    };
    processRawAttrs(el);
    console.log(el.attrs);  // [ { name: 'name', value: '"value"' } ] 

    14.函数式申明优先级比函数定义要高,

    function getName(){
        console.log(5);
    }
    var getName = function () {
        console.log(4);
    }
    getName();  //4, 涉及变量提示,第一个函数 foo 是一个完整的函数声明,没有涉及到赋值,赋值定义的函数会在执行期间覆盖变量提升。

     15.unicode和字符串相互转化

    // 字符串转unicode码
    "中".charCodeAt(0);  // 20013
    //unicode码转字符中
    String.fromCharCode(20013)   //
    //将unicode码转16进制
    "中".charCodeAt(0).toString(16)  //  4e2d

     16.

    var a = `
    `;
    console.log(a === '
    ');// true  
    为换行

     17. 获取一个dom节点的类的数组

    p.classList // classList为数组。添加样式。 p.classList.add('msg')
    //删除 p.classList.remove('msg')

     

     

  • 相关阅读:
    利用JBoss漏洞拿webshell方法
    jboss漏洞导致服务器中毒
    dubbo bug之 Please check registry access list (whitelist/blacklist)的分析与解决
    将list转为json字符串
    MySQL语句给字段值加1
    java int怎么转换为string
    HttpURLConnection如何添加请求头?
    eclipse下载egit插件,实现代码git同步问题
    eclipse编译项目用maven编译问题
    fastjson将java list转为json字符串
  • 原文地址:https://www.cnblogs.com/liuyinlei/p/7690737.html
Copyright © 2011-2022 走看看