引用型別
引用類型是一種資料結構,用於將資料和功能組織在一起。它也常被稱為類,但這種稱呼並不妥當。儘管 ECMAScript從技術上講是一門物件導向的語言,但它不具備傳統的物件導向語言所支援的類別和介面等基本結構。引用型別有時也被稱為物件定義,因為它們描述的是一類物件所具有的屬性和方法。
前面提到過,引用類型的值便是對象,在ECMAScript中,引用類型是一種資料結構,用於將資料和功能組織在一起,而對象則是某個特定引用類型的實例。
var a=new Object();
上面便宣告了一個引用型別為Object的實例,並將這個實例儲存到變數a中,也就是說這個變數實際上並不是包含了這個實例本身,而是指向這個實例的指標。
對於Object類型,常用物件字面量表示法來建立實例 既var a={name:"Nick",age:20}這樣做的優點是給人封裝的感覺。而物件的存取則是使用了點表示法或方括號表示法。 a.name等價於a["name"],注意此處的"name"是以字串表示的。
對於Array型,則可以使用陣列字面量表示法。
對於Array型,可以使用length來改變陣列的長度。 (從陣列的末端新增或移除項目)
偵測陣列的方法是Array.isArray(value)方法
轉換方法:toString()轉換成以“,”分割各項的一個字串。 valueOf(),傳回的依舊是數組。 toLocaleString()可以用下例實作。
var p1={ toString:function(){return "guo";}, toLocaleString:function(){return "yuzhe";} } var p2={ toString:function(){return "song";}, toLocaleString:function(){return "hap";} } var p=[p1,p2]; alert(p); //guo,song alert(p.toLocaleString()); //yuzhe,hap
可見 alert在輸出前先呼叫了toString()方法,此外還有join()方法,用於以指定的符號返回成字串 ,其預設的(不設定參數)為“,”。
棧方法:push()在末尾新增項,傳回數組長度。 pop()在最後刪除項,傳回刪除項。
隊列方法:shift()溢位數組第一項,傳回該項。 unshift()在首段新增項,傳回數組長度。
重排序方法(傳回值為陣列):
reverse()反轉順序。 a[length-1]=a[0]
sort()升序排序法 預設的sort()是以ASCII排序的,而非我們認為的數字大小,所以比較大小需要這樣使用
function compare(no1,no2){ if(no1<no2){ return -1;} else if(no1>no2){ return 1;} else{ return 0;} } var a=[1,2,3,4,6,5]; a.sort(compare); alert(a)
若想產生降序效果,只需反轉if語句。
操作方法:
concat()創建了一個副本,對原數組無影響,作用是添加接受的參數到數組的末尾。
slice()建立一個副本,接受1或2個參數(傳回項目的起始和結束位置,不包含結束位置),在只有一個參數的情況下,傳回從指定位置到結尾所有項目。若參數為負數,則結果為length+arguments,若結束位置小於起始位置,則傳回空數組。
splice():1.刪除方法--指定兩個參數,刪除的第一項的位置和刪除的項數。
2.插入方法--指定三個參數,起始位置,0(要刪除的數量),要插入的項。
3.替換方法--指定三個參數,起始位置,刪除的個數,要插入的項
插入/替換的位置是起始位置。
位置方法:
indexOf()傳回要尋找的項目的陣列下表,沒有則傳回-1.參數:要尋找的項目和(可選的)找出起點位置的索引(下標)。
lastIndexOf()是indexOf()的逆序。
迭代方法:
2個參數:要運行的函數和(可選的)的作用域,傳入這些方法中的函數需要有三個參數(item(數組項的值),index(該項的位置),array (數組物件本身)).
every()對數組中的每一項運行給定函數,每一項都回傳ture 則傳回true
filter()傳回會傳回true的項組成的陣列
forEach()對每一項運行給定函數,無回傳值
map()傳回每次函數執行結果所組成的陣列
some()若有一項為true,則回傳true
<script> var a=[1,2,3,4]; var b=a.every(function(item,index,array){ return item>2; }); alert(b); //false </script>
归并方法:
reduce()从数字第一项开始遍历,reduceRight()从数组最后一项开始遍历
使用reduce()来求数组中的所有和
<script> var a=[1,2,3,4]; var b=a.reduce(function(prve,cur,index,array){ return prve+cur; }); alert(b); //10
第一次执行时,prev为1,cur为2 ,第二次执行时,prev为3,cur为3。
以上所述是小编给大家介绍的JavaScript入门教程之引用类型的相关内容,希望对大家有所帮助!

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 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.

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'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.

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 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.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

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.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software