javascript 有趣的JS

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 有趣的JS相关的知识,希望对你有一定的参考价值。


Array.from( document.querySelectorAll("button") ).filter(bt => bt.innerText == 'Resolve conversation').forEach(bt => bt.click())

(function(){
    function GetNodesByQuery(querySelector) {
        const nodes = document.querySelectorAll(querySelector);
        return Array.from(nodes);
    }

    function AddCheckboxToElement(element) {
        var checkbox = document.createElement('input');
        checkbox.setAttribute('type', 'checkbox');
        element.appendChild(checkbox);
    }

    function AddCheckboxesToGithubCommitMessages(){
        var commitMessages = GetNodesByQuery('.commit-message') ;    
        commitMessages.forEach(cm => AddCheckboxToElement(cm));    
    }

    AddCheckboxesToGithubCommitMessages();
})();

(function(){
    function GetNodesByQuery(querySelector) {
        const nodes = document.querySelectorAll(querySelector);
        return Array.from(nodes);
    }

    function CreateCheckbox() {
        var checkbox = document.createElement('input');
        checkbox.setAttribute('type', 'checkbox');
        return checkbox;
    }

    function CreateTextInput() {
        var input = document.createElement('input');
        input.setAttribute('type', 'text'); // input.type = 'text'
        // input.setAttribute('name', 'somethingunique');
        return input;
    }

    var elements = GetNodesByQuery('.file-info') ;    
    elements.forEach(el => {
        el.appendChild(CreateCheckbox()); el.appendChild(CreateTextInput()); }
    );
})();
// Needs work
var FibonacciModule = (function() {

    var fib;
    var n1 = 1;
    var n2 = 0;
    var nextFibonacci;

    function fib2AndBeyond(){
        fib = n1 + n2;
        n2 = n1;
        n1 = fib;

        return fib;
    }

    function fib1(){
        nextFibonacci = fib2AndBeyond;
        return 1;
    }

    function fib0(){
        nextFibonacci = fib1;
        return 0;
    }

    nextFibonacci = fib0;

    function wrapperFunction(){
        console.log("wrapperFunction");
        return nextFibonacci();
    }

    return {
        callFibonacciFunctionVariable: nextFibonacci, // permanently assigned to fib0
        callFib2AndBeyond: fib2AndBeyond, // permanently assigned to fib2AndBeyond
        callWrapperFunction: wrapperFunction // calls wrapperFunction 
    }
})();

/*
FibonacciModule.callFibonacciFunctionVariable();
FibonacciModule.callFib2AndBeyond();
FibonacciModule.callWrapperFunction();

*/

以上是关于javascript 有趣的JS的主要内容,如果未能解决你的问题,请参考以下文章

javascript语法基础_java教程

JavaScript惰性函数定义

有趣的Javascript:只需一个JS让万恶的IE5IE6IE7IE8全都支持H5原生Canvas绘图(有演示demo)

js 闭包 弊端

15 个有趣的 JavaScript 与 CSS 库

JavaScript 有趣的冷知识:tagged template literals