1.parseInt:
>>>parseInt('123dsfsd')
123
>>>parseInt('abc1.33')
NaN
>>>parseInt(12dsfds333')
12
parseInt(arg1,arg2):arg2是需要转换的进制如:8/10/16
省略arg2的情况下:
parseInt('0x...'):hexadecimal
parseInt('0...'):octal(8)
一般的认为是10进制
2.parseFloat: only one parameter
used likes parseInt
>>>parseFloat('s.bb2.33')
NaN
>>>parseFloat('1.33dfs3.222')
1.33
特别地:
parseFloat()understands exponents(指数) in the input (unlike paeseInt())
>>>parseFloat(123e-2)
1.23
3.isNaN:用于检验输入的值是否是一个能用于算术运算的合法值,常用来检验parseInt,parseFloat的转换结果
>>>isNaN(isNaN)
true
>>>isNaN(233)
false //是一个合法的指
>>>isNaN(parseInt('abc123'))
true
特别的:
NaN === NaN is false
4.isFinite:是否是无穷大
>>>isFinite(Infinity)
false
>>>isFinite(-Infinity)
false
>>>isFinite(23)
true
>>>isFinite(1e308)
true
>>>isFinite(1e309)
false
Javascript'biggest number is 1.79e+308.
5.encodeURI/decodeURI...encodeURIComponent/decodeURIComponent
对url中特定的内容进行转义.注意:escape/unescape have been deprecated(废弃)
5.alert:在用户关闭它以前,浏览器中的js都会停止执行,在ajax中,这样做是不好的.
6.Gloabl & local variable:
var a = 123;
function f() {
alert(a);
var a = 1;
alert(a);
}
//undefined
//1
局部的优先级大于全局,在这里,局部的a重写了全局的a,导致地一个alert的时候未定义,但a在这个函数的局部是确实存在的.
7.function are Data
可以想一般变量一样对函数进行复制和删除
>>>var sum = function (a,b) { return a+b;}
>>>var add = sum
>>>delete sum
true
>>>typeof sum  //返回变量类型array/object/function
"undefined"
>>>typeof add
"function"
>>>add(3,5)
8
8.Anonynous Functions(匿名)

• You can pass an anonymous function as a parameter to another function. The receiving function can do something useful with the function that you pass.

• You can define an anonymous function and execute it right away.

  • Callback Functions

Advantages:

  • They let you pass functions withot the need to name them(which means there are less global variables)更少的全部变量
  • You can delegate the responsibility of calling a function to another function(which means there is less code to write)你能指派一个回调函数给另一个函数,意味着你可以写更少的代码
  • they can help with performance

function multiplyByTwo(a, b, c) {
var i, ar = [];
for(i = 0; i < 3; i++) {
ar[i] = arguments[i] * 2;
}
return ar;
}
function addOne(a) {
return a + 1;
}

>>>var myarr = [];

>>>myarr = multiplyByTwo(10.20,30);

[20,40,60]

>>for(var i=0; i<3; i++){myarr[i] = addNoe(myarr[i]);}

myarr

[21,41,61]

Loops can be expensive if they go through a lot or repetitions

匿名函数的改进:

>>>myarr = mulitiplybyTwo(1,2,3,function(a){return a+1;});

[4,6,8]

  • Slef-invoking Functions:calling this functinon right after it was defined.

()();

(

function() {

alert('hello');

}

)();

you simply place an anonymous function definition inside parentheses(括号) followed by another set of parentheses. The second set basically says "execute now" and is also the place to put any parameters that your anonymous function might accept.

One good reason for using self-invoking anonymous functions is to have some work done without creating global variables. A drawback(缺陷), of course, is that you cannot execute the same function twice (unless you put it inside a loop or another function). This makes the anonymous self-invoking functions best suited for one-off(一次性) or initialization tasks.

  • Inner (Private) Functions

Bearing in mind that a function is just like any other value.

function a() {

function b() {......}

}

a() is gloable function, and then b() is a local function, which canot accessible outside a(); likes a private function

Advantage

    • You keep the global namespace clean (smaller chance of naming collisions).减少命名空间的冲突
    • Privacy—you expose(暴露) only the functions you decide to the "outside world"(外部接口),keeping to yourself functionality that is not meant to be consumed(消耗) by the rest of the application.(让自己的功能不至于被外界的程序或则应用猜测到)
  • Function that Return Functions

前面说过,指明return的时候函数会返回相应的值/或则数组.当没指明时通常是undefined.同样,我们可以返回一个函数!

function a(){

alert('A');

return function () {

alert('B');

};

}

>>>var newFunc = a();

>>>>newFunc();

//A

//B

若想立即执行返回的函数,还可以这么做

>>>a()();

  • Function,Rewrite Thyself(你自己)!

Countiuing with the previous example:

>>>a = a();

//A   (consider this as being the one-off preparatory work)

//B   (Redefine the global variable a,assigning a new function to it)

This is usefull when a function has some initial one-off woek to do, The function overwrites itself after the first it back to the function.

var a = function () {  //最重要的,a在执行一次后就变样了,便成了a -> actualWork();

function someSetup() { //private

var setup = 'done';

}

function actualWork() { //private

alert('Work-worky');

}

someSetup(); //调用私有函数

return actualWork;  //return actualWork();区别在于前者返回的是一个函数,后者返回的是函数执行的结果

}(); //self-invoking function  only excute once

好处:在解决浏览器的兼容性的问题中,根据不同的浏览器选择不同的环境,就像上面一样,在执行一次后a就变了.

转载于:https://www.cnblogs.com/Poised-flw/archive/2013/03/29/2999673.html

Javascript 笔记(1)----函数相关推荐

  1. 【JavaScript 笔记】— 函数高级(变量作用域、解构赋值、方法、高阶函数、闭包、箭头函数、generator)

    JavaScript个人笔记 变量作用域 变量提升 全局对象 windows 命名空间 局部作用域 常量 解构赋值 使用场景 方法 apply 装饰器 高阶函数(Array) map reduce m ...

  2. 《JavaScript语言精粹》学习笔记(函数(2))

    <JavaScript语言精粹>学习笔记(函数(2)) 函数(Functions) 参数(Arguments) 当参数被调用时,会得到一个"免费"的参数数组argume ...

  3. javascript笔记:深入分析javascript里对象的创建(上)续篇

    今天回来一看我的博客居然有这么多人推荐真是开心极了,看来大家对我的研究有了认可,写博客的动力越来越大了,而且我发现写javascript在博客园里比较受欢迎,写java的受众似乎少多了,可能博客园里j ...

  4. JavaScript 笔记Day1

    <JavaScript 笔记> JavaScript 是属于网络的脚本语言! JavaScript 是世界上最流行的编程语言.这门语言可用于 HTML 和 web,更可广泛用于服务器.PC ...

  5. [Effective JavaScript 笔记]第27条:使用闭包而不是字符串来封装代码

    函数是一种将代码作为数据结构存储的便利方式,代码之后可以被执行.这使得富有表现力的高阶函数抽象如map和forEach成为可能.它也是js异步I/O方法的核心.与此同时,也可以将代码表示为字符串的形式 ...

  6. Web前端JavaScript笔记(4)节点

    如何获取元素节点的属性: 在Web前端JavaScript笔记(3)对象中,介绍了访问行间属性的方法,除此之外,系统还提供了三个方法访问元素的属性: 1. setAttribute: 2. getAt ...

  7. 【JavaScript笔记 · 基础篇(五)】Array全家桶(引用数据类型中的数组 / Array对象 / Array.prototype)

    文章目录 一. 引用数据类型中的数组 1.1 概述 1.2 初始化 1.2.1 字面量 1.2.2 构造函数模式 1.3 访问 1.4 length属性 1.5 数组遍历 1.6 类数组对象 1.6. ...

  8. 狂神Javascript笔记

    学习狂神JavaScript所记录的笔记 Javascript笔记 一.前端核心分析 1.1.概述 Soc原则:关注点分离原则 Vue 的核心库只关注视图层,方便与第三方库或既有项目整合. HTML ...

  9. JavaScript笔记(狂神说)

    JavaScript笔记(狂神说) 本文章根据b站狂神说javascript视频教程整理 视频链接:https://www.bilibili.com/video/BV1JJ41177di?from=s ...

最新文章

  1. 在EXT中前后台传数据的方式
  2. 《高性能网站建设指南》笔记-2 规则1——减少HTTP请求
  3. 卧槽!两大抓包神器的视频教程来啦!
  4. created写法_vue.js中created方法作用
  5. MyBatis学习后篇
  6. Android 测试点归纳总结
  7. [1077]ZooKeeper下载安装(Windows版本)
  8. 计算机存储容量的基本单位pb,pb存储单位是什么
  9. Vin码识别/车架号识别
  10. 创建视图,修改视图,修改视图数据
  11. O2O口号容易运营难
  12. meta-inf文件夹以及MANIFEST.MF文件的作用
  13. java 去除引号_java如何用replaceAll去除字符串中的引号
  14. 如何查看自己加入过的微信群
  15. altair 8800_Python数据可视化场景的戏剧性浏览(包括ggpy和Altair)
  16. 清除浮动的五种方法详解
  17. 2021研报目录更新
  18. 【概念卡片】误判心理学(一)
  19. Java小游戏教程,一小时学会贪吃蛇大作战,制作属于自己的游戏
  20. python数组赋值给另一个数组_使用numpy数组为另一个数组赋值

热门文章

  1. DCMTK:比较DICOM图像并计算差异指标
  2. DCMTK:将DICOM文件的内容转换为XML格式
  3. Qt Creator建立和运行
  4. OpenGL Distance Field文本的实例
  5. OpenGL Julia Fractal(Julia分形)的实例
  6. OpenGL 纹理Textures
  7. C++实现对链表的选择排序算法(附完整源码)
  8. QT实现RSS新闻阅读器
  9. lvm 多个硬盘合成一个_「ECS最佳实践」基于多块云盘构建LVM逻辑卷
  10. 9.LDA(线性判别分析)