js入门(二十):函数

函数

什么是函数
JavaScript 语言中的函数就是一段可以重复执行的代码块。简单来说,函数只需要定义一次,但可以调用一次或多次。如下示例代码所示:

//定义一个fun函数
function fun(){
	console.log('这是一个函数.')
}
//调用fun函数
fun()

函数定义使用function

调用()

有参函数定义以及调用

function add(a, b){
	console.log(a + b)
}
add(1, 2)

return 语句
返回用户所需要的返回值

预定义函数

在这里插入图片描述
eval() 函数

console.log(eval('4 + 4'))

eval() 函数的作用是将传入到该函数中的字符串当做 JavaScript 语言的代码进行执行。

字符编码与解码

var uri = 'http://www.example.com/前端开发工程师'

var encode = encodeURI(uri)
console.log(encode)

var decode = decodeURI(encode)
console.log(decode)
//http://www.example.com/%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88
//http://www.example.com/前端开发工程师

对中文进行编码以及解码

var uri = 'http://www.example.com/前端开发工程师'

var encode = encodeURIComponent(uri)
console.log(encode)

var decode = decodeURIComponent(encode)
console.log(decode)
//http%3A%2F%2Fwww.example.com%2F%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88
// http://www.example.com/前端开发工程师

对全部内容进行编码解码

发布了51 篇原创文章 · 获赞 8 · 访问量 1815

猜你喜欢

转载自blog.csdn.net/weixin_42393424/article/details/104327758
今日推荐