javascript 原型方法归纳 ------非Array篇

Function

Function.prototype.apply()

fun.apply(thisArg, [argsArray])

Function.prototype.call():
fun.call(thisArg[, arg1[, arg2[, …]]])

Number

Number.prototype.toFixed():
numObj.toFixed(digits) 转换成小数模式,参数为小数点位数

Number.prototype.toString():
numObj.toString(radix) 转换成字符串,参数为进制数

Object

Object.prototype.hasOwnProperty()
返回true or false,对原型链中属性返回false

以下Object属性为ESC5属性
Object.keys()
返回kay组成的数组

Object.freeze()
冻结一个对象,使其不可被操作

Object.create()
Object.create(proto [, propertiesObject ])
根据特定原型创建新对象

使用Object.create实现的继承

// Shape - superclass
function Shape() {
  this.x = 0;
  this.y = 0;
}

// superclass method
Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

// Rectangle - subclass
function Rectangle() {
  Shape.call(this); // call super constructor.
}

// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();

rect instanceof Rectangle // true.
rect instanceof Shape // true.

rect.move(1, 1); // Outputs, "Shape moved."

RegExp

RegExp.prototype.exec()
返回结果数组或null

RegExp.prototype.test()
返回true or false

String

String.prototype.charAt(index)
返回特定索引的字符

String.prototype.indexOf()
str.indexOf(searchValue[, fromIndex])
返回特定字符的位置索引

"Blue Whale".indexOf("Blue");     // returns  0
"Blue Whale".indexOf("Blute");    // returns -1
"Blue Whale".indexOf("Whale", 0); // returns  5
"Blue Whale".indexOf("Whale", 5); // returns  5
"Blue Whale".indexOf("", 9);      // returns  9
"Blue Whale".indexOf("", 10);     // returns 10
"Blue Whale".indexOf("", 11);     // returns 10

反向为String.prototype.lastIndexOf()

String.prototype.match(regexp)
根据正则查询,返回查询结果数组

String.prototype.replace()
str.replace(regexp|substr, newSubStr|function[, flags]);
返回替换过的新数组

String.prototype.slice()
str.slice(beginSlice[, endSlice])
类似Array的slice,返回字符串片段

String.prototype.split()
str.split([separator][, limit])
字符串打散为数组

var digits = '0123456789'
var a = digits.split('',5)
//return ['0','1','2','3','4']

以下ESC5属性
String.prototype.trim() 裁切前后空格

整理自
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

    原文作者:fishenal
    原文地址: https://segmentfault.com/a/1190000000492532
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞