变量
data() { return { flag: false, form: { title: '', subtitle: "", }, } }
方法
beforeRouteLeave(to, from, next) { //let one= JSON.parse(JSON.stringify(this.form)); let one = JSON.stringify({title: "1", subtitle: "2"}); let two = JSON.stringify({title: "1", subtitle: "3"}); console.log(window.performance.navigation.type); console.log(window.history.length); if (one !== two && !this.flag) { next(false); setTimeout(() => { this.Confirm("数据没有保存,确定要退出吗?", () => { this.flag = true; if (window.performance.navigation.type === 2) { window.history.go(-1) } else if (window.performance.navigation.type === 1) { this.$router.push({path: to.path}) } }, () => { }); }, 500) } else { next() } }
// confirm Confirm(text,ok,close){ this.$confirm(text, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { if(ok){ ok() } }).catch(() => { if(close){ close() } }); },
数组去重
const arr = ['张三', '张三', '三张三'] // set 自带去重 let set = new Set(arr) console.log(set) // [ '张三', '三张三' ] console.log(Array.from(set)) let person = [ {id: 0, name: '小明'}, {id: 1, name: '小张'}, {id: 2, name: '小李'}, {id: 3, name: '小孙'}, {id: 1, name: '小周'}, {id: 2, name: '小陈'} ] // 数组对象去重 let obj = {} // 设置cur默认类型为数组,并且初始值为空的数组 let peon = person.reduce((cur, next) => { obj[next.id] ? '' : obj[next.id] = true && cur.push(next) return cur }, []) console.log(peon)
eval的使用
var o = eval("({name:'kaka',age:19})") console.log(o.name)
var str1 = '{ "name": "kaka", "sex": "man" }'; var obj = JSON.parse(str1); //由JSON字符串转换为JSON对象 console.log(obj); console.log(typeof (obj)); var obj2 = eval('(' + str1 + ')'); //由JSON字符串转换为JSON对象 console.log(obj2); console.log(typeof (obj2)); var last = JSON.stringify(obj); //将JSON对象转化为JSON字符 console.log(last); console.log(typeof (last));
Funtion
function foo() { console.log("foo") } // 等价于 var func = Function("console.log("foo")"); func() var func = Function(foo()); func() function cmp(a, b, c) { return Math.max(a, b, c) } var max = new Function('a', 'b', 'c', 'return cmp(a, b, c)') console.log(max(11, 22, 33));
function Foo() { } // foo.prototype.sayHello = function (a) { // console.log(a) // } Foo.prototype = { func: function () { console.log("func") }, sayHello: function (a) { console.log(a) } }; var p = new Foo(); p.sayHello("sayHello"); p.func(); var o = { go: function () { console.log("go") } }; var o1 = Object.create(o); o1.go(); var list = Object.create([]); list.push("A"); list.push("B", "C", "D", "E", "F", "G"); for (let i = 0; i < list.length; i++) { console.log("" + i + "" + ": " + list[i]) }
函数科里化(高阶函数)
function foo() { var num = Math.random(); function func() { return num; } return func; } var f = foo(); var res1 = f(); var res2 = f(); console.log(res1); console.log(res2); function goo() { var o = {name: "kaka"}; return function () { return o; } } var g = goo(); var ret = g(); console.log(ret.name); // 函数科里化(高阶函数) console.log(goo()()); function hoo() { var num = Math.random(); return { get_num: function () { return num; }, set_num: function (value) { num = value; } } } var t = hoo(); var num = t.get_num(); console.log(num); t.set_num(1); num = t.get_num(); console.log(num);