Professional JS(5.5.5-function properties and methods&&5.6primitive wrapper type)

一.Function Properties and Method
1每个函数都包含两个属性:length/prototype
①The length peoperty indicates the number of named arguments that the function expects.

②The prototype is the actual location of all instance methods for reference types,meaning methods such as the toString() and valueOf() actually exist on the prototype and are then accessed from the object instances.In ECMAScript 5,the prototype property is not numerable and so will not be found using for-in.

2.每个函数都包含两个非继承而来的方法:apply()/call()/bind()
①apply()接收两个参数:the value of [this] inside the function, and an array of arguments.This second argument may be an instanceof Array,but it can also be the arguments Object.

②call()也接收两个参数,第一个参数也是this,其余参数需一一列出:
sum.call(this,num1,num2)

③两个方法的用途都是在特定的作用域中调用函数。

*④In strict mode,the [this] value of a function called without a context object is not coerced(强制) to window.Instead,[this] becomes undefined unless explicitly set by either attaching the function to an object or using apply() or call().

window.color='red';
var o={color : 'blue'};
function sayColor(){
console.log(this.color);
}
sayColor();//red
o.sayColor=sayColor;
o.sayColor();//blue

window.color='red';
var o={color : 'blue'};
function sayColor(){
console.log(this.color);
}
sayColor();//red
sayColor.call(window);//red
sayColor.call(this);//red
sayColor.call(o);//blue

传递参数并非是apply()和call()方法的真正用武之地,真正强大之处在于能够扩展函数赖以运行的作用域。

使用apply()和call()方法扩充作用域的最大好处:避免了对象与方法的耦合关系【变成了函数与方法的耦合】。

⑦ECMAScript 5中函数还定义了一个方法bind()
a)作用:创建一个函数的实例。

b)

window.color='red';
var o={color : 'blue'};
function sayColor(){
console.log(this.color);
}
var objectSayColor=sayColor.bind(o);
objectSayColor();//blue

sayColor()函数调用bind()方法,并传入o参数,随后创建了objectSayColor()函数,objectSayColor的this值为o,因此即使在全局作用域中调用该函数,仍显示”blue”.

c)支持bind()方法的浏览器有:IE9+,FireFox4+,Safari 5.1+,Opera 12,Chrome.

二.基本包装类型
1.Primitive Wrapper Type
①Every time a primitive value is read,an object of the corresponding primitive wrapper type is created behind the scenes,allowing access to any number of methods for manipulating the data.For example:

var s1='some text';
var s2=s1.substring(2);
s2;//"me text"

在这个过程中s2: string→String(object)→string

②当第二个语句访问s1时,访问过程处于一种读取模式,也就是要在内存中读取这个字符串的值。在读取过程中,后台进行三步操作:
a)创建一个String类型的实例:var s1=new String(‘some text’);
b)在实例上调用指定的方法:var s2=s1.substring(2);
c)销毁该实例:s1=null;

③The major difference between reference types and primitive wrapper types is the lifetime of the object.When you instantiate(实例化) a reference type using the new operator,it stays in memory until it goes out of scope.whereas automatically created primitive wrapper objects exist for only one line of code,before they are destroyed.This means that properties and methods cannot be added at runtime.

var s1='some text';
s1.color='red';
s1.color;//undefined

④Object构造函数会根据传入值的类型返回相应基本包装类型的实例:
a)string→Stirng

var obj=new Object('some text');
obj instanceof String;//true

b)number→Number

var a=new Object(123456);
a instanceof Number;//true

c)boolean→Boolean

var b=new Object(true);
b instanceof Boolean;//true
var c=new Object(0);
c instanceof Boolean;//false

⑤Keep in mind that calling a primitive wrapper constructor using new is not the same as calling the casting function(转型函数) of the same name.(Number)

var value='25';
var number=Number(value);
typeof(number);//"number"
var obj=new Number(value);
typeof(obj);//"object"

2.Boolean Type
①The Boolean type is the reference type corresponding to the Boolean values.

②var booleanObject=new Boolean(true);

var falseObject=new Boolean(false);
var result=falseObejct&&true;
result;//true---布尔表达式中的所有对象都会被转换为true
var falseValue=false;
result=falseValue&&true;
result;//false

typeof(falseObject);//"object"
typeif(falseValue);//"boolean"

falseObject instanceof Boolean;//true
falseObject instanceof Object;//true
falseValue instanceof Boolean;//false

3.Number Type—-toFixed()/toExponential()/toPrecision()
①The Number type is the reference type for numeric values.

②var numberObject=new Number(111);

③Number类型还提供一些用于将数值格式化为字符串的方法—toFixed()

var num=10;
num.toFixed(6);//"10.000000"

④toFixed()方法会按照指定的小数位返回数值的字符串形式。而且能够四舍五入(双向),因此toFixed()非常适合处理货币值。*toFixed() digits argument must be between 0 and 20.

⑤第二种方法—toExponential()指数
作用:以指数表示法返回数值的字符串形式。

var num=1000;
num.toExponential(3);//"1.000e+3"
var num=1000000;
num.toExponential(3);//"1.000e+6"

⑥第三种方法—toPrecision()—表示某个数值最合适的格式

var num=999;
num.toPrecision(1);//"1e+3"
num.toPrecision(2);//"1.0e+3"
num.toPrecision(3);//"999"
num.toPrecision(4);//"999.0"
num.toPrecision(5);//"999.00"
toPrecision() argument must be between 1 and 21.

var numberObject=new Number(10);
var numberValue=10;
typeof(numberObject);//"object"
typeof(numberValue);//"number"
numberObject instanceof Number;//true
numberValue instanceof Number;//false

4.String Type
①The String type is the object representation for strings and is created using the string constructor.

var stringObject=new String('Hello world');

②三种字符方法:charAt()/charCodeAt()/[]
var stringValue=’Hello world’;
a)stringValue.charAt(1);//”e”
b)stringValue.charCodeAt(1);//101—“e”的字符编码
c)stringValue[1];//”e”

③字符串操作方法:concat()—slice()—substr()—substring()
a)concat()
1.

var stringValue='Hello ';
var result=stringValue.concat('world');
result;//"Hello world"
stringValue;//"Hello "

concat()方法可以接收任意多个参数,即可拼接任意多个字符串。

b)slice()—substr()—substring()
三者都返回被操作字符串的一个子字符串,只返回一个基本类型的字符串值,对原始字符串没有任何影响。

var stringValue='Hello world';
stringValue.slice(3);//"lo world"
stringValue.substr(3);//"lo world"
stringValue.substring(3);/"lo world"
stringValue.slice(3,7);//"lo w"
stringValue.substr(3,7);//"lo worl"---参数b代表字符个数。
stringValue.substring(3,7);//"lo w"

c)如果slice()—substr()—substring()的传入参数为负值怎么办?
1.slice()—-a+length—-b+length
2.substr()—-a+length—-0
3.substring()—-0—–0

var stringValue='Hello world';
stringValue.slice(-2);//"ld"
stringValue.substr(-2);//"ld"
stringValue.substring(-2);//"Hello world"
stringValue.slice(-9,-2);//"llo wor"(2,9)
stringValue.substr(-9,-2);//""(2,0)
stringValue.substring(-9,-2);//""(0,0)
stringValue.substring(2,-2);//"He"(0,2)

④String Location Methods—indexOf()头→尾—-lastIndexOf()尾→头

a)

var stringValue='Hello world';
stringValue.indexOf('o');//4
stringValue.lastIndexOf('o');//7(原始顺序均是从头到尾数)

如果’o’在该字符串只出现过一次,那么indexOf()与lastIndexOf()返回相同的位置。

b)

var stringValue='Hello world';
stringValue.indexOf('o',6);//7
stringValue.lastIndexOf('o',6);//4

第二个参数表示从字符串中哪个位置开始搜索。

c)

var stringValue='lemon help let learn';
var positions=new Array();
var pos=stringValue.indexOf('e');
while(pos>-1){
positions.push(pos);
pos=stringValue.indexOf('e',pos+1);
}
positions;//[1,7,12,16]

⑤trim()方法—-修建—trimLeft()—trimRight()
a)作用:创建一个字符串的副本,并删除前置和后缀的所有空格。
b)由于trim()方法返回的是字符串的副本,所以原始字符串保持不变。

⑥字符串大小写转换方法:to(Locale)LowerCase()—to(Locale)UpperCase()

var stringValue='HeLLo wOrLd';
stringValue.toLowerCase();//"hello world"
stringValue.toLocaleLowerCase();//"hello world"
stringValue.toUpperCase();//"HELLO WORLD"
stringValue.toLocaleUpperCase();//"HELLO WORLD"

⑦字符串的模式匹配方法—match()—search()—replace()—split()

a)回顾:

var text='cat,bat,sat,fat';
var pattern1=/.at/;
var matches=pattern1.exec(text);
matches;//["cat", index: 0, input: "cat,bat,sat,fat"]
matches.index;//0
matches[0];//"cat"
pattern1.lastIndex;//0

b)match() Method

var text='cat,bat,sat,fat';
var pattern1=/.at/;
var matches=text.match(pattern1);
matches;//["cat", index: 0, input: "cat,bat,sat,fat"]
matches.index;//0
matches[0];//"cat"
pattern1.lastIndex;//0

c)search() Method

var text='cat,bat.sat,fat';
var pattern=/at/;
var pos=text.search(pattern);
pos;//1

search()方法始终是从字符串从左至右查找。

d)replace() Method
1.接收两个参数a,b—a:RegExp对象或一个字符串—b:一个字符串或一个函数function(match,pos,originalText)
2.

var text='cat,bat,sat,fat';
var result=text.replace('at','ond');//a:字符串---b:字符串
result;//"cond,bat,sat,fat"
result=text.replace(/at/g,'ond');//a:RegExp对象---b:字符串
result;//"cond,bond,sond,fond"

3.

var text='cat,bat,sat,fat';
result=text.replace(/(.at)/g,'word($1)');
result;//"word(cat),word(bat),word(sat),word(fat)"

‘$1’是匹配第一个捕获组的字符串。—“cat,bat,sat,fat”

e)split() Method
a)用途:基于指定的分隔符将一个字符串分割成多个字符串,并将结果放于数组中。—第二个参数指定数组大小。

b)

var colorText='red,green,blue,orange';
colorText.split(',');//["red","green","blue","orange"]
colorText.split(',',2);//["red","green"]
colorText.split(/[^\,]+/);//]["", ",", ",", ",", ""]

⑧localeCompare() Method
a)

var stringValue='orange';
stringValue.localeCompare('white');//-1---'o'排在'w'前面
stringValue.localeCompare('orange');//0
stringValue.localeCompare('blue');//1---'o'排在'b'后面

***b)

var value='orange';
var a='The string is "orange" is equal to the string "'+value+'".';
a;//"The string is "orange" is equal to the string "orange"."

⑨fromCharCode() Method
a)用途:接收一或多个字符编码,将其转化为一个字符串。
b)charCodeAt()方法的逆运算

String.fromCharCode(104,101,108,108,111);//"hello"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值