前端开发js常用代码

web前端与游戏开发
前端游戏开发书籍
前端可以开发游戏吗

javascript 代码

app.controller(‘loginCtrl’, [
    ‘$scope’,
    ‘$http’,
    function($scope, $http) {
        $scope.op = {}
        $scope.errmsg = ”
        $scope.op.loginName = localStorage.loginName //getCookie(“sUser”);
        var loginURL = ‘doLogin.do’
        $scope.submit = function() {
            $scope.op.loginName = $(‘.loginName’).val()
            $scope.op.password = $(‘.password’).val()
            if ($scope.op.password && $scope.op.loginName) {
                $http({
                    method: ‘POST’,
                    reponseType: ‘json’,
                    url: loginURL,
                    data: $scope.op,
                }).success(function(data) {
                    if (data) {
                        var jj = angular.fromJson(data)
                        if (jj.state) {
                            location.href = ‘index.do#/’
                            localStorage.removeItem(‘isFirstRefresh’)
                        } else {
                            $scope.errmsg = jj.msg
                            showConfirmBox()
                            changeImg()
                        }
                    }
                })
                // setCookie(“sUser”,$scope.op.loginName);
                localStorage.loginName = $scope.op.loginName
                // location.href=”index.do”;
            } else {
                $scope.errmsg = ‘请正确输入账号和密码!’
                showConfirmBox()
                changeImg()
            }
        }
        $scope.f_forgetPwd = function() {
            $scope.errmsg = ‘忘记密码,请联系管理员!’
            showConfirmBox()
        }
    },
])
if (localStorage.isFirstRefresh === undefined)
    localStorage.isFirstRefresh = ‘true’
if (localStorage.isFirstRefresh === ‘true’) {
    localStorage.isFirstRefresh = ‘false’
    window.location.reload()
}

javascript 代码

//驼峰
var str6 = ‘- _moz-transform’
function camelize(str) {
    return str.replace(/[-_\s]+(.)?/g, function(match, val) {
        //console.log(val);
        return val ? val.toUpperCase() : ”
    })
}
console.log(camelize(str6))

javascript 代码

//将每个单词的首字母转换为大写
var str5 = ‘i love javascript’
function change(str) {
    return str.toLowerCase().replace(/(^|\s)\w/g, function(val) {
        //val代表子项(?:^|\s)
        return val.toUpperCase()
    })
}
console.log(change(str5))

javascript 代码

var arr = [3, 6, 7, 2, 4, 6, 4]
//选择排序
function chooseSort(arr) {
    if (!arr instanceof Array) return arr
    if (arr.length === 1) return arr
    var min = arr[0],
        index = 0
    for (var i = 1, len = arr.length; i < len; i++) {
        if (min > arr[i]) {
            min = arr[i]
            index = i // 保存下最小值的索引,好用来后面删除它
        }
    }
    var minVal = arr.splice(index, 1)
    //console.log(minVal);
    // 通过递归,将这一轮中的最小值合并下一轮的最小值,直到arr的length为1停止递归。
    return minVal.concat(chooseSort(arr))
}
console.log(chooseSort(arr))

javascript 代码

//格式化日期
console.log(formatDate(new Date(), ‘yyyy-MM-dd hh:mm’))
function formatDate(date, fmt) {
    var preg = /(y+)/
    if (preg.test(fmt)) {
        console.log(RegExp.$1.length)
        fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + ”).substr(4 – RegExp.$1.length),
        )
        //console.log(fmt)
    }
    //console.log(fmt);
    let o = {
        ‘M+’: date.getMonth() + 1,
        ‘d+’: date.getDate(),
        ‘h+’: date.getHours(),
        ‘m+’: date.getMinutes(),
        ‘s+’: date.getSeconds(),
    }
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + ”
            console.log(RegExp.$1)
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length === 1 ? str : padLeftZero(str),
            )
        }
    }
    return fmt
}

javascript 代码

//数组的扁平化,就是将一个嵌套多层的数组 array (嵌套可以是任何层数)转换为只有一层的数组
function flatten(arr) {
    if (!arr instanceof Array) return arr
    var result = []
    for (var i = 0, len = arr.length; i < len; i++) {
        if (Array.isArray(arr[i])) {
            //如果是一个数组
            result = result.concat(flatten(arr[i]))
        } else {
            result.push(arr[i])
        }
    }
    return result
}

javascript 代码

/*type
1:首字母大写
2:首页母小写
3:大小写转换
4:全部大写
5:全部小写
* */
function changeCase(str, type) {
    function ToggleCase(str) {
        var itemText = ”
        str.split(”).forEach(function(item) {
            if (/^([a-z]+)/.test(item)) {
                itemText += item.toUpperCase() //大写
            } else if (/^([A-Z]+)/.test(item)) {
                itemText += item.toLowerCase() //小写
            } else {
                itemText += item
            }
        })
        return itemText
    }
    switch (type) {
        case 1:
            return str.replace(/^(\w)(\w+)/, function(v, v1, v2) {
                //第一个参数正则整体,v1,v2代表正则一个子项
                return v1.toUpperCase() + v2.toLowerCase()
            })
        case 2:
            return str.replace(/^(\w)(\w+)/, function(v, v1, v2) {
                return v1.toLowerCase() + v2.toUpperCase()
            })
        case 3:
            return ToggleCase(str)
        case 4:
            return str.toUpperCase()
        case 5:
            return str.toLowerCase()
        default:
            return str
    }
}
console.log(changeCase(‘html’, 3))

javascript 代码

//去除空格 type 1-所有空格 2-前后空格 3-前空格 4-后空格
function trim(str, type) {
    if (typeof str !== ‘string’) return str
    switch (parseInt(type)) {
        case 1:
            return str.replace(/\s+/g, ”)
        case 2:
            return str.replace(/(^\s*)|(\s*$)/g, ”)
        case 3:
            return str.replace(/(^\s*)/g, ”)
        case 3:
            return str.replace(/(\s*$)/g, ”)
        default:
            return str
    }
}
console.log(trim(‘ html ‘, 2))

javascript 代码

var Arr = [10, [2, 5, [8, 7]]]
//数组的扁平化,就是将一个嵌套多层的数组 array (嵌套可以是任何层数)转换为只有一层的数组
function flatten(arr) {
    if (!arr instanceof Array) return arr
    var result = []
    for (var i = 0, len = arr.length; i < len; i++) {
        if (Array.isArray(arr[i])) {
            //如果是一个数组
            result = result.concat(flatten(arr[i]))
        } else {
            result.push(arr[i])
        }
    }
    return result
}
console.log(flatten(Arr))
前端知识开发游戏
网易游戏 前端开发
游戏开发 前端后端
» 本文来自:前端开发者 » 《前端开发js常用代码》
» 本文链接地址:https://www.rokub.com/8089.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!