smm临时笔记

2018-10-14  本文已影响0人  ft207741
function F(){
        document.write(F.prototype.constructor===F)
    }
    f()// true

    // 构造函数function(.prototype)-> 原型对象(.constructor)->构造函数
    // 构造函数(new)-> 实例object (.__proto__)-> 原型对象(.constructor)->构造函数
构造函数和原型对象和 实例对象之间的关系 图片.png
    function F1(){
        this.name = "nameF1";
    }
    F1.prototype.getf1name = function(){
        return this.name;
    }
    function F2() {
        this.name = "nameF2";
    }
    F2.prototype = new F1();
    F2.prototype.getf2name = function(){
        return this.name;
    }
    F2.prototype.getf1name = function(){
        return "newname";
    }

    var f2instance = new F2();
    document.write(f2instance.getf1name());// newname; 重新定义继承自祖辈的方法, 会覆盖原方法

    var f1instance = new F1();
    document.write(f1instance.getf1name()); //nameF1;重新定义继承自祖辈的方法,不会改变祖辈自己的调用
    function F1(){
        this.colors = ["red", "blue", "green"];
    }
    function F2(){}
    F2.prototype = new F1();
    var instance1 = new F2();
    instance1.colors.push("black");
    document.write(instance1.colors); //"red,blue,green,black"
    var instance2 = new F2();
    document.write(instance2.colors); //"red,blue,green,black"
    function F1(){
        this.colors = ["red", "blue", "green"];
    }
    function F2(){
        F1.call(this);
    }
    var instance1 = new F2();
    instance1.colors.push("black");
    document.write(instance1.colors); //"red,blue,green,black"
    var instance2 = new F2();
    document.write(instance2.colors); //"red,blue,green"
    var person = {
        name: "Nicholas",
        friends: ["Shelby", "Court", "Van"]
    };
    var anotherPerson = Object.create(person, {
        name: {
            value: "Greg"
        }
    });
    alert(anotherPerson.name); //"Greg"
    function createAnother(original){
        var clone = object(original); //通过调用函数创建一个新对象
        clone.sayHi = function(){ //以某种方式来增强这个对象
            alert("hi");
        };
        return clone; //返回这个对象
    }
    function inheritPrototype(subType, superType){
        var prototype = Object.create(superType.prototype); //创建对象
        prototype.constructor = subType; //增强对象
        subType.prototype = prototype; //指定对象
    };


    function F1(name){
        this.name = name;
        this.colors = ["red", "blue", "green"];
    };
    F1.prototype.sayName = function(){
        alert(this.name);
    };
    function F2(name, age){
        F1.call(this, name);
        this.age = age;
    }
    inheritPrototype(F2, F1);
    F2.prototype.sayAge = function(){
        alert(this.age);
    };

    document.write(F2)



page 190/206/208

对于元素节点, nodeName 中保存的始终都是元素的标签名,而 nodeValue 的值则始终为 null。
每个节点都有一个 childNodes 属性,其中保存着一个 NodeList 对象, 使用 Array.prototype.slice()方法可以将其转换为数组。
hasChildNodes()
appendChild()
insertBefore()
replaceChild()
removeChild()
cloneNode()

parentNode
childNodes
firstChild 和 lastChild
nextSibling 和 previousSibling
ownerDocument

2. outerHTML 属性

  <div id="testinner">
        <p>This is a <strong>paragraph</strong> with a list following it.</p>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
        </ul>
    </div>
    <div id="testouter">
        <p>This is a <strong>paragraph</strong> with a list following it.</p>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
        </ul>
    </div>

    <script>
        div = document.getElementById("testinner")
        div.innerHTML = "<p>This is another paragraph.</p>";
        alert("this is inner>"+div.innerHTML)  // this is inner><p>This is another paragraph.</p>

        div = document.getElementById("testouter")
        div.outerHTML = "<p>This is another paragraph.</p>";
        alert("this is outer>"+div.outerHTML)

        // this is outer><div id="testouter">
        // <p>This is a <strong>paragraph</strong> with a list following it.</p>
        // <ul>
        // <li>Item 1</li>
        // <li>Item 2</li>
        // <li>Item 3</li>
        // </ul>
        // </div>
    </script>
        //     <div id="container"><div id="d">This is a div.</div></div>
        container = document.getElementById("container");
        d = document.getElementById("d");
        alert(container.firstChild.nodeName); // logs "DIV"

        d.outerHTML = "<p>This paragraph replaced the original div.</p>";
        alert(container.firstChild.nodeName); // logs "P"
        alert(d.nodeName); // logs "P"
var div = document.createElement("div");
div.outerHTML = "<div class=\"test\">test</div>";
console.log(div.outerHTML); // output: "<div></div>"

4. 内存与性能问题

        myul = document.getElementById("myul")
        for (var i=0, len=5; i < len; i++){
            myul.innerHTML += "<li>" + i + "</li>"; //要避免这种频繁操作!!
        }

        var itemsHtml = "";
        for (var i=0, len=5; i < len; i++){
            itemsHtml += "<li>" + i + "</li>";
        }
        myul.innerHTML = itemsHtml;  //这个例子的效率要高得多,因为它只对 innerHTML 执行了一次赋值操作。
上一篇 下一篇

猜你喜欢

热点阅读