JS笔记

2018-04-11  本文已影响0人  酥枫
  1. js中,调用Object.prototype.toString.call(a)和调用a.toString()结果不同,除非a是一个对象。
  2. 解构赋值
let {a,b,...c}={a:1,b:2,u:3,v:4};
console.log(c);//{u:3,v:4}
var a=(function(x){
    delete x;
    return x;
})(2);
console.log(a);//2
var a=1;
console.log(global.a);//undefined
console.log(delete a);//false
console.log(typeof a);//number
console.log(a);//1
/*原因:使用var声明的变量有一个名为[[Configurable]]的特性,
且被设置为false,因此不能通过delete操作符删除,会返回false*/
a=1;
console.log(global.a);//1
console.log(delete a);//true
console.log(typeof a);//undefined
console.log(a);//ReferenceError: a is not defined
var a={},
    b={key:'b'},
    c={key:'c'};
a[b]=123;
a[c]=456;
console.log(a[b]);//456
console.log(a);//Object {[object Object]: 456}

原因:a[variable]相当于给对象a添加了一个属性,而属性的key就是variable.toString()的结果,Object类型的b和c的b.toString()c.toString()的结果均为"[object Object]",所以a[c]就会把a[b]覆盖

  1. 在任何值上调用Object原生的toString()方法,都会返回一个"[object NativeConstructorName]"格式的字符串。每个类在内部都有一个[[Class]]属性,这个属性中就指定流量上述字符串中的构造函数名。(与第一点中的照应)。
  2. 在对非数值应用一元加操作符时,该操作符会像Number()转型函数一样对这个值执行转换。
  3. Number()转型函数规则
  4. 添加动态脚本
    document.write(),接收一个字符串,且<script>...</script>要写成<script>...<\/script>
    ②创建<script>元素,指定src(或者script.appendChild(document.createTextNode("js代码"))),document.body.appendChild()
  5. 添加动态样式
    ①创建<link>元素,指定href(或者创建style元素style.appendChild(document.createTextNode("css代码"))),document.head.appendChild()
  6. 有关添加、删除、获取属性的四种方式
    ele.getAttribute("name") setAttribute("class") remove...
    ②通过HTML属性如ele.idele.titleele.styleele.className
    ③通过ele.attributes属性获得NodeList,ele.attributes.getNamedItem("id").nodeValue OR ele.attributes["id"].nodeValue
var attr=document.createAttribute("name");
attr.value="aaaaa";
ele.setAttributeNode(attr);
  1. 返回NodeLIst(包括NodeListHTMLCollectionNamedNodeMap)的方法:childNodesgetElementsByTagName()getElementsByName()element.attributesgetElementsByClassName()childrenform.elements
  2. 为单例对象创建私有变量
var sigleton=function(){
    var privateVar=10;
    function privateFunc(){
        return false;
    }

    return{
        publicVar:"hahaha",
        publicFunc:function(){
            privateVar++;
            return privateFunc;
        }
    }
}();
console.log(sigleton.privateVar);//undefined
console.log(sigleton.publicVar);//"hahaha"
console.log(sigleton.publicFunc()());//false

  1. 惰性载入函数两种方法:
    ①在函数第一次执行时将函数重新覆盖为一个适当的函数(在第一次执行时损失性能)
function createXHR(){
    if(xxx){
        createXHR=function(){
            //...
        };
    }else if(yyy){
        createXHR=function(){
            //...
        };
    }else{
        createXHR=function(){
            //...
        };
    }
    return createXHR();
}

②在函数第一次声明函数时就指定适当的函数(在第一次加载时损失性能)

var createXHR=function(){
    if(xxx){
        return function(){
            //...
        }
    }else if(yyy){
        return function(){
            //...
        }
    }else{
        return function(){
            //...
        }
    }
}();
  1. 函数柯里化
  1. 函数绑定
  2. Object.create()的内部实现:
function object(o){
    function F(){}
    F.prototype=o;
    return new F();
}

除此之外,Object.create()还有一个可选的第二个参数,一个对象,表示新对象的可选属性。e.g.

var aPerson=Object.create(Person.prototype,{
    name:{
        value:"Xiaoming"
    }
});
  1. 创建对象的方式
  1. 继承方式
    ①原型链继承
    ②构造函数继承
    ③组合继承(原型链+构造函数)
    ④原型式继承
    ⑤寄生组合式继承
  2. e.getAttribute(propName)e.propName的区别和联系

页面重绘(repaint)和页面回流(reflow):

  1. 输入URL到显示界面中发生了什么
  2. 发生内存泄露的情况
  1. cookie、sessionStorage、localStorage区别
  1. CSS中position四个值的区别(static、relative、absolute、fixed)
  1. ES新特性:
  1. Git基本操作
  2. 数组去重、数组乱序、深拷贝、浅拷贝
上一篇 下一篇

猜你喜欢

热点阅读