search
HomeWeb Front-endJS TutorialDetailed explanation of built-in objects in js object-oriented (code)

本篇文章给大家带来的内容是关于js面向对象中内置对象的详解(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

方法

String

  • 获取字符串长度Length属性

  • 连接字符串:concat(String, String)

  • 获取索引值:indexOf(String)

  • 根据索引值获取单个字符:charAt(Index)

  • 从起始索引fromIndex开始截取长度length的字符串:substr(fromIndex,length)

  • 截取 起始索引startIndex  到  结束索引endIndex的子字符串,结果包含startIndex处的字符,不包含endIndex处的字符:substring(startIndex,endIndex)/slice(startIndex,endIndex)

  • 按给定字符串分割,返回分割后的多个字符串组成的字符串数组:split(String)

  • 使用选择的分隔符将一个数组合并为一个字符串:join(String)

  • 大小写转换:toLowerCase()/toUpperCase()

  • replace

  • 判断是否包含指定字符串:contains(String)

  • 判断是否为空:isEmpty()

  • html编码和解码:escapeHTML() unescapeHTML()
    replace的用法:

//$符的使用
myString=myString.replace(/(Marvin)/g,"<font color=red>$1</font>");
//函数
function test(str){
    return "<font color=&#39;red&#39;>"+str+"</font>"}
myString=myString.replace(/(Marvin)/g,test);

Array

  • 添加和删除:shift unshift【从数组开头开始添加或者删除】,pop push【从数组末尾添加或者删除】

  • 数组合并,参数添加到原数组中,返回新的数组:concat()

  • 排序: sort() 从小到大 reverse() 从大到小

  • 数组截取: slice(start,end)

  • 数组拼接成字符串:join(separator)

  • splice()

Date

date = new Date(2015, 2, 27, 12, 59, 59);
- date.toLocaleString(): 2015年3月27日 12:59:59
- date.toLocaleString(): 2015年3月27日 12:59:00
- date.toLocaleString(): 2015年3月27日 12:00:00
- date.toLocaleString(): 2015年3月27日 12:00:00
- date.toLocaleString(): 2015年3月27日 12:00:00

date = new Date(“month dd,yyyy hh:mm:ss”):date = new Date(‘2014-12-25’)

  • getFullYear()

  • getYear()

  • getMonth()

  • getDate(): 获取几号   - 0 - 31 比如25

  • getDay(): 获取星期几 - 比如星期3的3

  • getHours()

  • getMinutes()

  • getSeconds()

  • getMilliseconds()

  • getTime(): 获取相对于1970-01-01的毫秒值

Function

  • Arguments

  • Length

  • Caller属性 获取调用当前函数的函数。caller属性只有当函数正在执行时才被定义

  • Callee属性 返回正被执行的 Function 对象,即指定的 Function 对象的正文

  • constructor 属性  就是用来构造对象实例的函数引用。

  • Prototype属性获取对象的原型。每一个构造函数都有一个prototype属性,指向另一个对象。这个对象的所有属性和方法,都会被构造函数的实例继承。这意味着,我们可以把那些不变的属性和方法,直接定义在prototype对象上。

  • Apply方法  调用函数,并用指定对象替换函数的this值,同时用指定数组替换函数的参数。

  • Call方法  调用一个对象的方法,用另一个对象替换当前对象。

  • Bind方法  对于给定函数,创建具有与原始函数相同的主体的绑定函数。

  • toString  返回对象的字符串表示形式。

call::借用,伪数组

//call的用法:借用()
var myclass={getAllPersonNumbers:function(age){return age}};
var student={
      getDetail:function(){
          return {name:&#39;Marvin&#39;,age:20}
      }
  };
myclass.getAllPersonNumbers.call(student)
//call的用法:借用(传参)
var myclass={getAllPersonNumbers:function(){return 33}};
var student={
      getDetail:function(){
          return {name:&#39;Marvin&#39;,age:20}
      }
  };
myclass.getAllPersonNumbers.call(student,20)
//call 的用法伪数组(将伪数组改为真 的数组)
var json = {1:&#39;&#39;,2:&#39;&#39;,length:2}
  • call方法会修改this的指针,在call后改变被借用的函数的this指针指向自身。

//call将指向全局的this的指针指向了自身student
myclass.getAllPersonNumbers.call(student,20)
  • 伪数组:获取到的dom就是一个伪数组,jquery获取到的也是一个伪数组

var ss = {0: &#39;Marvin&#39;, 1: &#39;lili&#39;}Array.prototype.slice.call(ss)/
/能将具有length属性的对象转成数组
=> [Marvin , lili]

apply:

//循环式获取最大值
function getMax(arr){
    var arrLen=arr.length;    
    for(var i=0,ret=arr[0];i<arrLen;i++){
        ret=Math.max(ret,arr[i]);
    }return ret;
}
//max()只能传递离散的数值
//获取数组中的最大值
myclass.getAllStudentsNumbers.apply(student,[10,200])return 
Math.max.call(null,1,2,3,4,5) === return Math.max.apply(null,arr);
//合并数组
Array.prototype.push.apply(arr1,arr2)

相关推荐:

JS中的内置对象Array详解

Detailed explanation of JS’s built-in object String

The above is the detailed content of Detailed explanation of built-in objects in js object-oriented (code). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment