eval

JSON.parse()和eval()的区别

风流意气都作罢 提交于 2019-11-28 17:04:02
json格式非常受欢迎,而解析json的方式通常用JSON.parse()但是eval()方法也可以解析,这两者之间有什么区别呢? JSON.parse()之可以解析json格式的数据,并且会对要解析的字符串进行格式检查,如果格式不正确则不进行解析,而eval()则可以解析任何字符串,eval是不安全的。 比如下面的字符串: var str = 'alert(1000.toString())' ; eval ( str ); JSON . parse ( str ); 用eval可以解析,并且会弹出对话框,而用JSON.parse()则解析不了。 其实alert并没有什么坏处,可怕的是如果用恶意用户在json字符串中注入了向页面插入木马链接的脚本,用eval也是可以操作的,而用JSON.parse()则不必担心这个问题。 注意:某些低级的浏览器尚不支持JSON.parse(),可以到 https://github.com/douglascrockford/JSON-js/blob/master/json2.js 下载。 来源: oschina 链接: https://my.oschina.net/u/1266171/blog/410187

How to modify a global variable within a function in bash?

社会主义新天地 提交于 2019-11-28 16:47:11
I'm working with this: GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) I have a script like below: #!/bin/bash e=2 function test1() { e=4 echo "hello" } test1 echo "$e" Which returns: hello 4 But if I assign the result of the function to a variable, the global variable e is not modified: #!/bin/bash e=2 function test1() { e=4 echo "hello" } ret=$(test1) echo "$ret" echo "$e" Returns: hello 2 I've heard of the use of eval in this case, so I did this in test1 : eval 'e=4' But the same result. Could you explain me why it is not modified? How could I save the echo of the test1

redis 正确实现分布式锁的正确方式

陌路散爱 提交于 2019-11-28 16:11:53
前言 最近在自己所管理的项目中,发现redis加锁的方式不对,在高并发的情况有问题。故在网上找搜索了一把相关资料。发现好多都是互相抄袭的,很多都是有缺陷的。好多还在用redis 的 setnx命令来实现分布式锁。其实redis 中的set命令本身就已经集成了setnx命令的功能了,而且比其还强大。这里,我使用 redis-cli 客户端 结合lua脚本 原生 的实现redis 分布式锁。 准备材料 redis-server redis-cli LUA 与 REDIS 的关系 从 redis2.6.0 开始, redis 内部就内置了lua解释器。lua也就是成了redis扩展的一种解决方案了。 格式: eval scripts num [keys ...] [argv ...] eval : redis中执行lua脚本的命令 scripts : lua脚本,不必一定得是lua函数 num : keys 的个数,用于区分 keys 和 argv ;没有keys,则写0 keys: 可变数组,一般用于表示redis 的键,下标从1 开始 argv: 可变数组,一般用于表示redis 的值,下标从1 开始 redis 与 lua 的交互 分两种情况: redis 执行 lua 脚本 : 使用 redis中的 eval命令 即可 lua & redis lua 接收 redis

Python 如何将字符串转为字典

别等时光非礼了梦想. 提交于 2019-11-28 15:55:32
Python 如何将字符串转为字典 在工作中遇到一个小问题,需要将一个 python 的字符串转为字典,比如字符串: user_info = '{"name" : "john", "gender" : "male", "age": 28}' 我们想把它转为下面的字典: user_dict = {"name" : "john", "gender" : "male", "age": 28} 有以下几种方法: 1、通过 json 来转换 >>> import json >>> user_info= '{"name" : "john", "gender" : "male", "age": 28}' >>> user_dict = json.loads(user_info) >>> user_dict {u'gender': u'male', u'age': 28, u'name': u'john'} 但是使用 json 进行转换存在一个潜在的问题。 由于 json 语法规定 数组或对象之中的字符串必须使用双引号,不能使用单引号 ( 官网 上有一段描述是 “A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes” )

python内置函数与匿名函数

拈花ヽ惹草 提交于 2019-11-28 15:45:06
一.匿名函数   匿名即没有名字。 #普通函数 def func(x,y,z=1): return x+y+z #匿名函数及没有名字,只有参数列表与return的值表达式,用:分隔 lambda x,y,z=1:x+y+z #与函数有相同的作用域,但是匿名意味着引用计数为0,使用一次就释放,除非让其有名字 func=lambda x,y,z=1:x+y+z func(1,2,3) #让其有名字就没有意义   匿名函数与有名函数的对比 #有名函数与匿名函数的对比 有名函数:循环使用,保存了名字,通过名字就可以重复引用函数功能 匿名函数:一次性使用,随时随时定义 应用:max,min,sorted,map,reduce,filter 二. 内置函数与匿名函数的使用   python中有很多的内置函数,比如常用的max、range、type、getattr、setattr、open等。更多内置函数: https://docs.python.org/3/library/functions.html?highlight=built#ascii 2.1 max、min   max与min的用法一样,用于获取一个可迭代对象中的最大值与最小值,可以key指定匿名函数自定义返回值用于max、min比较的依据。 # max,min使用方法一致 iterable = [1, 5, 3, 2, 7]

Given the following LISP eval function - what is required to add defmacro?

谁说我不能喝 提交于 2019-11-28 15:40:22
Given the following definition of the LISP eval function - what is required to add the defmacro function? (Or even just evaluate a macro) (defun null. (x) (eq x '())) (defun and. (x y) (cond (x (cond (y 't) ('t '()))) ('t '()))) (defun not. (x) (cond (x '()) ('t 't))) (defun append. (x y) (cond ((null. x) y) ('t (cons (car x) (append. (cdr x) y))))) (defun list. (x y) (cons x (cons y '()))) (defun pair. (x y) (cond ((and. (null. x) (null. y)) '()) ((and. (not. (atom x)) (not. (atom y))) (cons (list. (car x) (car y)) (pair. (cdr x) (cdr y)))))) (defun assoc. (x y) (cond ((eq (caar y) x) (cadar

Webpack中的sourceMap配置

偶尔善良 提交于 2019-11-28 15:22:24
---------------------------------------webpack.config.js----------------------------------------------------------module.exports = { mode:'development', // 开发环境 development // devtool:'cheap-module-eval-source-map', // 生产环境 production // devtool:'cheap-module-source-map', devtool:'cheap-module-eval-source-map', entry: { main: './src/index.js' }, output: { filename: '[name].js', path: path.resolve(__dirname,'dist') }, plugins:[new HtmlWebpackPlugin({ template:'src/index.html' }),new CleanWebpackPlugin(['dist'])], module: { rules: [ { test: /\.jpg$/, //这个配置是重点 use: { loader:"file-loader",

前端面试题 之 JavaScript

∥☆過路亽.° 提交于 2019-11-28 14:32:53
昨天我们一起分享了关于html和css的面试题 《前端面试题之Html和CSS》 , 今天我们来分享关于javascript有关的面试题。我面试的时候最害怕面试官问我js了,因为我真心不擅长这个。不过我在努力的学习中。 本宝宝第一次面试的时候比这个还紧张呢!!! 1.介绍js的基本数据类型 Undefined 、 Null 、 Boolean 、 Number 、 String 2.js有哪些内置对象? 数据封装类对象:Object、Array、Boolean、Number 和 String 其他对象:Function、Arguments、Math、Date、RegExp、Error 3.this对象的理解 this总是指向函数的直接调用者(而非间接调用者); 如果有new关键字,this指向new出来的那个对象; 在事件中,this指向触发这个事件的对象,特殊的是,IE中的attachEvent中的this总是指向全局对象Window; 4.eval是做什么的? 它的功能是把对应的字符串解析成JS代码并运行; 应该避免使用eval,不安全,非常耗性能(2次,一次解析成js语句,一次执行)。 由JSON字符串转换为JSON对象的时候可以用eval,var obj =eval(‘(‘+ str +’)’); 5.DOM怎样添加、移除、移动、复制、创建和查找节点 // 创建新节点

How to use Single Quotes in Eval Format String

喜你入骨 提交于 2019-11-28 13:35:13
I've got a Repeater and its SqlDatasource nested inside a Gridview TemplatedField. The Repeater's datasource SelectCommand is set using the FormatString of an Eval from the Gridview. The SelectCommand has a WHERE clause which is to compare a string. Because I have already used the single and double quotes, I am having trouble delimiting the string in the SQL WHERE clause. How do I add single quotes inside an Eval FormatString? I have tried using ' Replace '. I have tried using ' Special Characters ' (... WHERE StringField = '{0}' ...) No luck so far. I appreciate any help you may be able to

Is there a performance gain in including <script> tags as opposed to using eval?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 13:21:01
I have seen a lot of suggestions about how one should add code dynamically like so (source) : var myScript = document.createElement("script"); myScript.setAttribute("type","text/javascript"); myScript.innerHTML += 'alert("Hello");'; document.body.appendChild(myScript); As opposed to eval like so eval('alert("Hello");'); People complain about performance drops and security issues with eval , but I can't imagine how adding <script> tags would be any faster or any more secure. EDIT people would like to know why I am evaling something as trivial as alert("Hello") , here is why: I have a database