zoukankan      html  css  js  c++  java
  • JavaScript核心编程(代码片段)

    var a = function () {
        function someSetup() {
            var setup = 'done';
        }
        function actualWork() {
            alert('Worky-worky');
            //return true;
        }
        someSetup();
        return actualWork;
    }();
    
    a(); // 打印出了 Worky-worky
    function f() {
        var a = [];
        var i;
        
        for(i = 0; i < 3; i++) {
            a[i] = (function (x) {
                return function () {
                    return x;
                }
            })(i);
        }
        
        return a;
    }
    
    var a = f();
    console.log(a[0]()); // 0
    console.log(a[1]()); // 1
    console.log(a[2]()); // 2
    var getValue, setValue;
    
    (function () {
        var secret = 0;
        getValue = function () {
            return secret;
        };
        setValue = function (v) {
            secret = v;
        };
    })();
    
    console.log(getValue());
    setValue(2);
    console.log(getValue());
    // 迭代器
    function setup(x) {
        var i = 0;
        return function () {
            return x[i++];
        };
    }
    
    var next = setup(['a', 'b', 'c']);
    console.log(next()); // a
    console.log(next()); // b
    console.log(next()); // c
    // 构造函数
    function Hero() {
        this.occupation = 'Ninja';
    }
    
    var hero = new Hero();
    console.log(hero.occupation);
    function Hero(name) {
        this.name = name;
        this.occupation = 'Painer';
        this.whoAreYou = function () {
            return "I'm " + this.name + " and I'm a " + this.occupation;
        };
    }
    
    var hero = new Hero('Nico');
    document.writeln(hero.whoAreYou());
    (function (count) {
        if(count < 5) {
            alert(count);
            arguments.callee(++count);
        }
    })(1);
    // 正则表达式
    function replaceCallback(match) {
        return '_' + match.toLowerCase();
    }
    
    var s = 'HelloJavaScriptWorld';
    
    console.log(s.replace(/[A-Z]/g, replaceCallback)); // _hello_java_script_world
  • 相关阅读:
    超级女声杭州赛区7进5
    究竟怎么了?
    最近发现
    S2SH基于角色权限拦截
    基于S2SH的电子商务网站系统性能优化
    TSQL复习笔记(一)
    用户sa登录失败,该用户与可信sql server连接无关联
    SQL附加数据库报5120的错误的解决办法
    DotNet中配置文件的使用(一)
    JQuery中使用AJAX $.ajax(prop)方法详解
  • 原文地址:https://www.cnblogs.com/lqcdsns/p/6051213.html
Copyright © 2011-2022 走看看