资讯专栏INFORMATION COLUMN

红皮书(4):引用类型

CoorChice / 2498人阅读

摘要:类型类型重排序方法升序降序方法返回从参数指定位置开始到当前数组末尾的所有项。要注意的是,传递给构造函数的两个参数都是字符串不能把正则表达式字面量传递给构造函数。由于构造函数的模式参数是字符串,所以在某些情况下要对字符串进行双重转义。

Object类型 Array类型 重排序方法: compare

升序:

function compare(value1, value2){
    if (value1value2){
        return 1;
    } else{
       return 0;
    }
}
var values = [0,1,5,10,15];
values.sort(compare);
console.log(values); // [0,1,5,10,15]

降序:

function compare(value1, value2){
    if (value1value2){
        return -1;
    } else{
       return 0;
    }
}
slice

slice(start, end); slice()方法返回从参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起死和结束位置之间的项,但不包括结束位置的项。

var colors = ["red", "green", "blue", "yellow", "purple"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);

console.log(colors2); // green, blue, yellow, purple
console.log(colors3); // green, blue, yellow
splice

splice()有删除,插入,替换的功能

删除
需要两个参数,要删除的第一项的位置和要删除的项数。

var colors = ["red", "green", "blue"];
var removed = colors.splice(0,1);
console.log(colors); // greeen, blue
console.log(removed); // red

插入
需要三个参数:起始位置、0(要删除的项数)和要插入的项

var colors = ["red", "green", "blue"];
var removed = colors.splice(1,0,"yellow", "orange");
console.log(colors); // ["red", "yellow", "orange", "green", "blue"]
console.log(removed); // 返回空

替换
需要三个参数:起始位置、要删除的项数和要插入的任意数量的项。

var colors = ["red", "green", "blue"];
var removed = colors.splice(1,1,"yellow", "orange");
console.log(colors);  // ["red", "yellow", "orange", "blue"]
console.log(removed); // ["green"]
Date类型 RegExp类型
var pattern1 = /[bc]/i;
var pattern2 = new RegExp("[bc]at", "i");

pattern1和pattern2是两个完全等价的正则表达式。要注意的是,传递给RegExp构造函数的两个参数都是字符串(不能把正则表达式字面量传递给RegExp构造函数)。由于RegExp构造函数的模式参数是字符串,所以在某些情况下要对字符串进行双重转义。

var pattern1 = /[bc]/i;
var pattern2 = new RegExp("[bc]at", "i");
RegExp实例方法

exec

exec接收一个参数,即要应用模式的字符串,然后返回包含第一个匹配信息的数组。

var text = "cat, bat, sat, fat";
var pattern1 = /.at/;

var matches = pattern1.exec(text);
console.log(matches); // ["cat"]

match
match是字符串执行匹配正则表达式规则的方法,他的参数是正则表达

var text = "cat, bat, sat, fat";
var pattern1 = /.at/;

var matches2 = text.match(pattern1);
console.log(matches2); // ["cat"]

test
test()接收一个字符串参数

var text = "000-00-0000";
var pattern = /d{3}-d{2}-d{4}/;

if (pattern.test(text)){
    console.log("The pattern was matched"); // The pattern was matched
}
Function类型 函数内部属性

把arguments转为数组

(function() {
    var slice = Array.prototype.slice,
        aArguments = slice.apply(arguments);

        console.log(aArguments);
})(10, 20, 30);

arguments.callee

该属性是一个指针,指向拥有这个arguments对象的函数。当函数在严格模式下运行时,访问arguments.callee会导致错误。

函数属性和方法

length
length属性表示函数希望接收的命名参数的个数。

function sayName(name){
    alert(name);
}

function sum(num1,num2){
    return num1 + num2;
}

function sayHi(){
    alert("hi");
}

console.log(sayName.length); //1
console.log(sum.length); //2
console.log(sayHi.length); //0

prototype

call, apply

function sum(num1, num2){
    return num1 + num2;
}

function callSum1(num1,num2){
    return sum.apply(this,arguments);
}

function callSum2(num1, num2){
    return sum.apply(this, [num1, num2]); 
}

console.log(callSum1(10,10)); // 20
console.log(callSum2(10,10)); //20
window.color = "red";
var o = {color:"blue"};

function sayColor(){
    console.log(this.color);
}

sayColor(); // red

sayColor.call(this); // red
sayColor.call(window); // red
sayColor.call(o); // blue
基本包装类型
var value = "25";
var number = Number(value);
console.log(typeof number);
console.log(number instanceof Number);// false

var obj = new Number(value);
console.log(typeof obj);
console.log(obj instanceof Number);// true
Boolean类型
var falseObject = new Boolean(false);
var result = falseObject && true; // true  

//布尔表达式中的所有对象都会被转换为true, 因此falseObject对象在布尔表达式中代表的是true

console.log(result); // true

var falseValue = false;
result = falseValue && true;
console.log(result); //false

console.log(typeof falseObject); //object
console.log(typeof falseValue); // Boolean
console.log(falseObject instanceof Boolean); //true
console.log(falseValue instanceof Boolean); // false
Number类型
var numberObject = new Number(10);
var numberValue = 10;
console.log(typeof numberObject); // Object
console.log(typoef numberValue); // number
console.log(numberObject instanceof Number); // true
console.log(numberValue instanceof Number); // false
String类型 字符方法

charAt() charCodeAt()

charAt()方法以单字符字符串的形式返回给定位置的那个字符串。

charCodeAt()返回的是字符编码。

var stringValue = "hello world";
console.log(stringValue.charAt(1)); // e
console.log(stringValue.charCodeAt(1)); // 101
字符串操作方法

concat()

concat()用于将一或多个字符串拼接起来。

var stringValue = "hello ";
var result = stringValue.concat("world");
console.log(result); // hello world
console.log(stringValue); // hello

slice(start, end)
end 表示字符串到哪里结束。
如果传入的是负数,slice()方法会将传入的负值与字符串长度相加。

var str="Hello happy world!";
console.log(str.slice(6)); // happy world!
console.log(str.slice(6,11));// happy
console.log(str.slice(-3)); // ld!
console.log(str.slice(3, -4)); //lo happy wo 

substring(start, end)
如果传入的是负数, substring()会把所有字符参数都转换为0

var str="Hello happy world!";
console.log(str.substring(6)); // happy world!
console.log(str.substring(6,11));// happy
console.log(str.substring(-3)); // Hello happy world!
console.log(str.substring(3, -4)); //Hel

substr(start, length)
如果传入的是负数,substr()方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0

var str="Hello world!";
console.log(str.substr(3)); //lo world!
console.log(str.substr(3, 7)); //lo worl
console.log(str.substr(-3)); // ld!
console.log(str.substr(3, -3)); // 空字符串
字符串位置方法

indexOf() lastIndexOf()

var stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
console.log(stringValue.lastIndexOf("o")); //7

这两个方法都可以接收可选的第二个参数,表示从字符串中的哪个位置开始搜索。

var stringValue = "hello world";
console.log(stringValue.indexOf("o", 6)); // 7
console.log(stringValue.lastIndexOf("o", 6)); //4
字符串的模式匹配方法

match()

var text = "cat, bat, sat, fat";
var pattern = /.at/;

var matches = text.match(pattern);
console.log(matches.index); //0
console.log(matches[0]); // cat
console.log(pattern.lastIndex); //0

search()

var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
console.log(pos); // 1

replace()

var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
console.log(result); // cond, bat, sat, fat

var result = text.replace(/at/g, "ond");
console.log(result); // cond, bond, sond, fond
Global对象

URI编码方法
Global对象的encodeURI()和encodeURIComponent()方法可以对URI(Uniform Resources Identifiers,通用资源标识符)进行编码,以便发送给浏览器。

var url = "http://www.baidu.com/";
console.log(encodeURI(url));
console.log(encodeURIComponent(url));

encodeURI()和encodeURIComponent()方法对象的两个方法分别是decodeURI()和decodeURIComponent()

Math对象

random()方法

Math.random()方法返回介于0和1之间一个随机数,不包含0和1。对于某些站点来说,这个方法非常实用,因为可以利用它来随机显示一些名言和新闻事件。套用下面的公式,就可以利用Math.random()从某个整数范围内随机选择一个值。

值=Math.floor(Math.random()*可能值的总数+第一个可能的值)

例如:如果想选择一个1到10之间的数值,可以像下面这边编写代码:

var num = Math.floor(Math.random()*10+1);
function selectFrom(lowerValue,upperValue){
    var choice = upperValue - lowerValue + 1;
    return Math.floor(Math.random()*choice+lowerValue);
}
var num = selectFrom(2,10);
console.log(num);
var colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"];
var color = colors[selectFrom(0, colors.length-1)];
console.log(color);

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/87446.html

相关文章

  • 皮书(3):变量、作用域和内存问题

    摘要:局部变量只在函数执行过程中存在。此时,局部变量就没有存在的必要了,因此可以释放他们所占的内存以供他们使用。这一做法适合于大多数全局变量和局部变量的属性。 基本类型和引用类型的值 动态的属性 var person = new Object(); person.name = Nicholas; alert(person.name); // Nicholas var name = N...

    phodal 评论0 收藏0
  • 第三期:基于皮书「创建js对象的6种方法」再讨论(1)

    摘要:构造函数里的作用在的时候,所谓的作用域赋给新对象,就是使用了方法。如果我们不用,也可以实现指向我们想指向的对象构造函数当做普通函数使用,构造函数当做普通函数使用,这些变式其实都是在充分了解和的用法后,自然写出来的。 这一期所书写的目的并不是为了介绍创建对象的方法,在红皮书里面,例子其实很清楚。在这篇文章中,想讨论一下书中细节,并没有解释太清楚的地方。 工厂模式 //工厂模式书本「 fu...

    liaoyg8023 评论0 收藏0
  • 【翻译】理解 IPFS 白皮书 第1部分

    摘要:理解白皮书第部分原文链接哦不,白皮书加密货币区块链世界喜爱白皮书,也不例外。要理解的工作原理,最好一步一步地学习白皮书。从开始,白皮书中明确提到了三个实现。白皮书指出小值等于或小于直接存储在上。 理解 IPFS 白皮书 第 1 部分 原文链接:https://decentralized.blog/un... 哦不,白皮书! 加密货币 / 区块链世界喜爱白皮书,IPFS 也不例外。 它起...

    Ilikewhite 评论0 收藏0
  • 皮书(10):DOM扩展

    摘要:取得所有类中包含和的元素。类名的先后顺序无所谓取得为的元素中带有类名的所有元素焦点管理也添加了辅助管理焦点的功能。首先就是属性,这个属性始终会引用中当前获得了焦点的元素。另外就是新增了方法,这个方法用于确定文档是否获得了焦点。 选择符API querySelector()方法 // 取得body元素 var tbody = document.querySelector(body);...

    ASCH 评论0 收藏0
  • JS程序

    摘要:设计模式是以面向对象编程为基础的,的面向对象编程和传统的的面向对象编程有些差别,这让我一开始接触的时候感到十分痛苦,但是这只能靠自己慢慢积累慢慢思考。想继续了解设计模式必须要先搞懂面向对象编程,否则只会让你自己更痛苦。 JavaScript 中的构造函数 学习总结。知识只有分享才有存在的意义。 是时候替换你的 for 循环大法了~ 《小分享》JavaScript中数组的那些迭代方法~ ...

    melody_lql 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<