JavaScript中各种引用类型的常用操作方法小结

 更新时间:2016年05月05日 16:26:09   作者:小渝人儿  
这篇文章主要介绍了JavaScript中各种引用类型的常用操作方法小结,基本上都用实际代码进行展示,是整理得比较全面的学习笔记,需要的朋友可以参考下

Object类型

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

function compare(value1, value2){
  if (value1<value2){
    return -1;
  }
  if (value1>value2){
    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 (value1<value2){
    return 1;
  }
  if (value1>value2){
    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);

相关文章

  • 详解原生JS回到顶部

    详解原生JS回到顶部

    这篇文章主要介绍了JS回到顶部,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • js oncontextmenu事件使用详解

    js oncontextmenu事件使用详解

    这篇文章主要介绍了js oncontextmenu事件使用详解,需要的朋友可以参考下
    2017-03-03
  • 深入理解JS函数的参数(arguments)的使用

    深入理解JS函数的参数(arguments)的使用

    下面小编就为大家带来一篇深入理解JS函数的参数(arguments)的使用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • js时间比较示例分享(日期比较)

    js时间比较示例分享(日期比较)

    这篇文章主要介绍了js时间比较示例,代码简单,运行后可以看到结果,需要的朋友可以参考下
    2014-03-03
  • javascript学习笔记(十九) 节点的操作实现代码

    javascript学习笔记(十九) 节点的操作实现代码

    javascript学习笔记之节点的操作实现代码,包括节点的创建、添加、移除、替换、复制
    2012-06-06
  • 浅谈一下JavaScript与C++的差异

    浅谈一下JavaScript与C++的差异

    这篇文章主要介绍了浅谈一下JavaScript与C++的差异,本来两门语言各有各的战场,并没什么交集,但自从 Node.js 框架出现之后,JavaScript 就摆脱了浏览器的樊笼,开始渗透进入系统应用的领域
    2023-04-04
  • 新手入门常用代码集锦

    新手入门常用代码集锦

    新手入门常用代码集锦...
    2007-01-01
  • Javascript中的数据类型之旅

    Javascript中的数据类型之旅

    JavaScript 是一种弱类型或者说动态语言。这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定。这也意味着你可以使用同一个变量保存不同类型的数据。
    2015-10-10
  • JavaScript 变量命名规则

    JavaScript 变量命名规则

    学习js的朋友一定要知道和注意,其实每种语言都有它的命名规则。
    2009-09-09
  • JS常用函数使用指南

    JS常用函数使用指南

    本文汇总了107个常用的js使用的时候的注意事项,本来想凑足一百单八将的,结果还是差了一个,哈哈,以后有机会再补吧。
    2014-11-11

最新评论