Home Web Front-end JS Tutorial JavaScript introductory tutorial reference types_javascript skills

JavaScript introductory tutorial reference types_javascript skills

May 16, 2016 pm 03:02 PM

引用型別

引用類型是一種資料結構,用於將資料和功能組織在一起。它也常被稱為類,但這種稱呼並不妥當。儘管 ECMAScript從技術上講是一門物件導向的語言,但它不具備傳統的物件導向語言所支援的類別和介面等基本結構。引用型別有時也被稱為物件定義,因為它們描述的是一類物件所具有的屬性和方法。

前面提到過,引用類型的值便是對象,在ECMAScript中,引用類型是一種資料結構,用於將資料和功能組織在一起,而對象則是某個特定引用類型的實例。

var a=new Object(); 
Copy after login

  上面便宣告了一個引用型別為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 
Copy after login

可見 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)
Copy after login

若想產生降序效果,只需反轉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>
Copy after login

    归并方法:

        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
Copy after login

第一次执行时,prev为1,cur为2 ,第二次执行时,prev为3,cur为3。

以上所述是小编给大家介绍的JavaScript入门教程之引用类型的相关内容,希望对大家有所帮助!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

Simple JavaScript functions are used to check if a date is valid. function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] '/' bits[1] '/' bits[0]); return !!(d && (d.getMonth() 1) == bits[1] && d.getDate() == Number(bits[0])); } //test var

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

This article discusses how to use jQuery to obtain and set the inner margin and margin values ​​of DOM elements, especially the specific locations of the outer margin and inner margins of the element. While it is possible to set the inner and outer margins of an element using CSS, getting accurate values ​​can be tricky. // set up $("div.header").css("margin","10px"); $("div.header").css("padding","10px"); You might think this code is

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

This article explores ten exceptional jQuery tabs and accordions. The key difference between tabs and accordions lies in how their content panels are displayed and hidden. Let's delve into these ten examples. Related articles: 10 jQuery Tab Plugins

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

Discover ten exceptional jQuery plugins to elevate your website's dynamism and visual appeal! This curated collection offers diverse functionalities, from image animation to interactive galleries. Let's explore these powerful tools: Related Posts: 1

HTTP Debugging with Node and http-console HTTP Debugging with Node and http-console Mar 01, 2025 am 01:37 AM

http-console is a Node module that gives you a command-line interface for executing HTTP commands. It’s great for debugging and seeing exactly what is going on with your HTTP requests, regardless of whether they’re made against a web server, web serv

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

The following jQuery code snippet can be used to add scrollbars when the div content exceeds the container element area. (No demonstration, please copy it directly to Firebug) //D = document //W = window //$ = jQuery var contentArea = $(this), wintop = contentArea.scrollTop(), docheight = $(D).height(), winheight = $(W).height(), divheight = $('#c

See all articles