


Summary of common operation methods of various reference types in JavaScript_Basic knowledge
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);

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
