Professional JS(5.4-RegExp Type&5.5-Function Type)
来源:互联网 发布:iphone7没有4g网络 编辑:程序博客网 时间:2024/06/05 21:05
1.RegExp Type(正则表达式类型)
①var expression=/pattern(模式)/flags(标志);
②flags: g–global/i–case-insensitive/m-multiline.
③元字符必须转义(如:\)—( [ { \ ^ | ) ? * +. ] }
④两种创建正则表达式的方式:
a)字面量:var pattern1=/[bc]at/i;—匹配第一个”bat”或”cat”,不区分大小写。
b)RegExp构造函数,包含两个参数:a–要匹配的字符串模式,b–可选的标志
var pattern2=new RegExp(‘[bt]at’,’i’);//传递给RegExp构造函数的两个参数都是字符串。
⑤ECMAScript 5明确规定,使用正则表达式字面量必须像直接调用RegExp构造函数一样,每次都创建新的RegExp实例。
2.RegExp实例的属性
①global,ignoreCase,multiline均是布尔值,分别判断g/i/m标志。
②lastIndex,an integer indicating the character position where the next match will be attempted in the source string(搜索下一个匹配项的字符位置).This value always begins as 0.
③source:正则表达式的字符串表示,以字面量形式返回。
④字面量表示和RegExp构造函数。两者source属性一致,因此其保存的是规范形式的字符串(即字面量形式)
3.RegExp实例方法
①RegExp对象的主要方法:exec()—index,input
②exec()方法的作用:捕获组(capturing group)
③exec()方法接受一个参数—相当于一个范围,在其中捕获符合要求的内容(组)。
④如果是全局模式,每次调用exec()都会返回字符串中的下一个匹配项,直至字符串末尾
⑤正则表达式第二个方法是test(),接受一个字符串参数。当模式与该参数匹配的情况下返回true,反之false.
⑥用途:判断目标字符串与模式是否匹配,但不需要了解其具体文本内容,多用于if语句
4.RegExp构造函数属性
5.Function Type
①三种定义函数的方式
a)
function sum(num1,num2){ return num1+num2;}
b)
var sum=function(num1,num2){ return num1+num2;};
c)
var sum=new Function('num1','num2','return num1+num2');//not recommended
因为Function构造函数表示法会导致解析两次代码(第一次解析常规的代码,第二次解析传入构造函数中的字符串)
*②函数是对象,函数名是指针。
*③没有圆括号的函数名代表函数指针。
6.No Overloading(visited)
①
function addSomeNumber(num){ return num+100;}function addSomeNumber(num){ return num+200;}var result=addSomeNumber(100);//300
②
var addSomeNumber=function(num){ return num+100;};addSomeNumber(num){ return num+200;};var result=addSomeNumber(100);//300覆盖
7.Function Declarations versus Function Expressions
①
sum(10,10);function sum(num1,num2){ return num1+num2;}//20
解析器会率先读取函数声明,并使其在执行任何代码之前可用。—function declaration hoisting函数声明提升
②
sum(11,11);var sum=function(num1,num2){ return num1+num2;};//sum is not a function
③可同时使用函数声明与函数表达式,如var sum=function sum(){},safari出错。
8.Functions as values
①Because function names in ECMAScript are nothing more than variables,functions can be used anyplace any other value can be used.This means it’s possible not only pass a function into another function as an argument but also to return a function as the result of another function.
②
function callSomeFunction(someFunction,someArgument){ return someFunction(someArgument);}function add10(num){ return num+10;}var result1=callSomeFunction(add10,20);result1;//30function getGreeting(name){ return 'Hello, '+name;}var result2=callSomeFunction(getGreeting,'yyc');result2;//"Hello, yyc"
③
function createComparisonFunction(propertyName){ return function(object1,object2){ var value1=object1[propertyName]; var value2=object2[propertyName]; if(value1<value2){ return -1; }else if(value1>value2){ return 1; }else{ return 0; } };}var data=[{name:'yyc',age:21},{name:'balabala',age:25}];data.sort(createComparisonFunction('name'));data[0].name;//"balabala"data.sort(createComparisonFunction('age'));data[1].age;//25
9.Function internals
①函数内部有两个特殊的对象:arguments&this
②The arguments object is an array-like object that contains all of the arguments that were passed into the function.
③
function factorial(num){ if(num<=1){ return 1; }else{ return num*factorial(num-1); }}factorial(4);//24
④升级版:可用函数内部对象arguments的属性callee,解除执行函数与函数名factorial的紧密耦合。
function factorial(num){ if(num<=1){ return 1; }else{ return num*arguments.callee(num-1); }}factorial(5);//120
⑤解除函数执行与函数名紧密耦合的优势:
a)
function factorial(num){ if(num<=1){ return 1; }else{ return num*factorial(num-1); }}var trueFactorial=factorial;factorial=function(){ return 0;}trueFactorial(4);//0
b)
function factorial(num){ if(num<=1){ return 1; }else{ return num*arguments.callee(num-1); }}var trueFactorial=factorial;factorial=function(){ return 0;}trueFactorial(5);//120
⑥this对象
window.color='red';var a={color:'orange'};function sayColor(){ console.log(this.color);}sayColor();//reda.sayColor=sayColor;a.sayColor();//orange
*⑦Function names are simply variables containing pointers,so the global sayColor() function and o.sayColor() point to the same function even though they execute in different contexts.
⑧’caller’,’callee’ and ‘arguments’ properties may not be accessed on strict mode.”use strict”
⑨函数的caller属性
function funcA(){if(funcA.caller==null){ console.log('Called from the global'); }else{ console.log('Called from another function'); }}function funcB(){funcA();}funcA();//Called from the globalfuncB();/Called from another function
- Professional JS(5.4-RegExp Type&5.5-Function Type)
- Professional JS(5.5.5-function properties and methods&&5.6primitive wrapper type)
- Professional JS(三-statements&function)&(四-primitive&reference type)&黑画(5-another road)
- Professional JS(3.4.7The Object Type&3.5Operators)
- Professional JS(5.1-5.3---Object/Array/Date Type)
- Professional JS(3.4.5The Number Type&3.4.6The String Type)
- Aggregate Function Data Type
- C++11: function type
- static function FindObjectsOfType (type : Type) : Object[]
- Professional JS(三.Language basic-syntax&keywords&reserved&variables&half data type)&黑画(5)
- SharePoint 2013 IT Professional--Convert License Type
- SharePoint2013 IT Professional - Content Type Hub
- type
- type
- TYPE
- Type
- type()
- GD: Function and type reference
- ARKit demo品牌应用软件,十二居家具AR露真容!
- PAT b1006题解
- linux 服务器部署 lnamp 一nginx安装
- 浏览器兼容性问题大汇总
- Drupal开发培训Day4
- Professional JS(5.4-RegExp Type&5.5-Function Type)
- java语法基础
- Nginx和Tengine概述及安装、启动
- PAT b1007题解
- MapReduce整体流程
- secure boot原理
- C#网络编程-简单的通信源码
- 正则表达式
- Maven 打War包时报错,JDWP exit error JVMTI_ERROR_WRONG_PHASE(112)