ES5-19 變量聲命周期、垃圾回收原理、arguments

變量聲命周期

垃圾回收

  1. 找出不再使用的變量
  2. 釋放其占用內存
  3. 固定的時間間隔運行
  • 解除由于閉包產生的對fn AO的引用

標記清除

  • 排除全局變量、排除閉包引用的AO中的變量
  • 進入環境 → 離開環境
  • 常用

引用計數

  • 引用計數為0時清除
  • 對循環引用的情況,如果不手動接觸引用(a = null),則無法清除

arguments

屬性

  • 函數內部對應參數值的實參列表
  • 類數組對象 Array-like
  • 有屬性callee,指向它的宿主函數
  • 屬性Symbol(Symbol.iterator) 表示可迭代
  • constructor是Object
  • 有length屬性
  • 屬性下標從0開始
  • 沒有數組的內置方法
  • 注意:箭頭函數沒有arguments
function test(a, b) {console.log(arguments) // 1 2 3console.log(Object.prototype.toString.call(arguments)) // [object Arguments]console.log(Array.isArray(arguments)) // false
}
test(1, 2, 3,)

在這里插入圖片描述

普通對象是不可迭代的

在這里插入圖片描述

  • 三個參數,迭代到第四個便完成了
function* generator(args) {for (var key in args) {yield key}
}
var it = generator(obj)
console.log(it.next())
console.log(it.next())
console.log(it.next())
console.log(it.next())
function test(a, b) {var it = generator(arguments)console.log(it.next())console.log(it.next())console.log(it.next())console.log(it.next())
}
test(1, 2, 3)

在這里插入圖片描述

箭頭函數內使用實參(es6弱化了arguments)

  • 嚴格模式讓arguments和eval少了一些奇怪的行為。兩者在通常的代碼中都包含了很多奇怪的行為。

Strict mode makes arguments and eval less bizarrely magical. Both involve a considerable amount of magical behavior in normal code.

在這里插入圖片描述
在這里插入圖片描述

arguments的一些操作會阻止js引擎的優化

對參數使用slice會阻止某些JavaScript引擎中的優化 (比如 V8 - 更多信息)。如果你關心性能,嘗試通過遍歷arguments對象來構造一個新的數組。另一種方法是使用被忽視的Array構造函數作為一個函數
var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));

bluebird-petkaantonov

  • 多種方式將arguments轉為新數組返回
function test() {var arr = []for (var v of arguments) {arr.push(v)}// return arr// return arguments.length === 1 ? [arguments[0]] : Array.from(arguments)return arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)
}
console.log(test(1, 2, 3))

使用場景:

  1. 實參個數 > 形參個數
  2. 不定參數

形、實參的對應關系

Note: If you’re writing ES6 compatible code, then rest parameters should be preferred.
Note: “Array-like” means that arguments has a length property and properties indexed from zero, but it doesn’t have Array’s built-in methods like forEach() and map(). See §Description for details.

當非嚴格模式中的函數沒有包含剩余參數、默認參數和解構賦值,那么arguments對象中的值會跟蹤參數的值(反之亦然)。

1. 形參中但凡有一個有默認值, 形、實參不再對應

function test(a = 100, b, c) {arguments[1] = 1000console.log(b, arguments[1]) // 2 100
}
test(1, 2, 3)

不統一的具體表現

1. ES6形參默認值

  1. 當實參為undefined時,形參若有默認值,便取默認值
function test(a = 100, b, c) {arguments[0] = 1000console.log(a, arguments[0]) // 100 1000
}
test()
  1. 形參、實參并不統一
function test(a = 100, b, c) {console.log(a, arguments[0]) // 100 undefined
}
test()
  1. 修改形參,便不再取默認值
function test(a = 100, b, c) {a = 666console.log(a, arguments[0]) // 666 1
}
test(1)

2. ES6使用展開運算符

function test(...args) {arguments[0] = 100console.log(args[0], arguments[0]) // 1 100
}
test(1)

3. ES6參數解構

function test({ a, b, c }) {arguments[0]['a'] = 100console.log(a, arguments[0]['a']) // 1 100
}
test({ a: 1, b: 2, c: 3 })

MDN: 注意: 在嚴格模式下,arguments對象已與過往不同。arguments[@@iterator]不再與函數的實際形參之間共享,同時caller屬性也被移除。

4*. 嚴格模式下,怎么都不對應

function test(a, b, c) {'use strict'arguments[0] = 100console.log(a, arguments[0]) // 1 100
}
test(1)

5. ES5實參undefined

var test = function (a, b) {a = 1console.log(a, arguments[0]) // 1 undefined
}
test()
// 這個相當于es5無默認值
var test = function (a = undefined, b) {console.log(a, arguments[0]) // 100 100
}
test(100)

arguments

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/251145.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/251145.shtml
英文地址,請注明出處:http://en.pswp.cn/news/251145.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

bzoj 1801: [Ahoi2009]chess 中國象棋【dp】

注意到一行只能放012個炮&#xff0c;我們只需要知道列的狀態&#xff0c;不用狀壓行 所以設f[i][j][k]表示前i行有j列有1個炮&#xff0c;有k列有2個炮的方案數 然后分情況討論轉移就行了 #include<cstdio> #include<iostream> using namespace std; const int N1…

vue --- compoent妙用

首先利用寫一個靜態模板的組件 <div id "app"><my-arti></my-arti> </div> <script>Vue.component(my-arti,{template:<div style"border:1px solid black"><span>date:2019年06月14日</span><br>…

ES5-20 復習

3-1 變量單一聲明方式String Boolean undefined Number nullundefined nulltypeof(null) ‘object’typeof(方法) ‘function’typeof() 是運算符&#xff0c;不是數據類型 報錯0 -0 trueInfinity -Infinity falseNaN和誰都不等原始值沒有屬性 要打印屬性、調用方法得經過基…

eclipse中去掉警告提示

有時候我們要去掉這些不必要的提示 下面我們來設置去掉這些警告提示 轉載于:https://www.cnblogs.com/xiaostudy/p/9370016.html

vue --- vue-router

vue-router的CDN <script src "https://unpkg.com/vue-router2.5.3/dist/vue-router.js"></script>// 當然還需要導入vue的cdn <script src"https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>使用router-link(to)添加點擊鏈…

django-restframework使用

安裝restframework: pip install djangorestframework 修改項目settings.py: INSTALLED_APPS [django.contrib.admin,django.contrib.auth,django.contrib.contenttypes,django.contrib.sessions,django.contrib.messages,django.contrib.staticfiles,rest_framework, ]修改項…

JSON基礎與數據解析、JSON方法、AJAX初識

JSON JavaScript Object Notation js對象標記是對象&#xff0c;是輕量級數據交互的格式&#xff0c;不能有方法它基于 JavaScript 語法&#xff0c;但與之不同&#xff1a;JavaScript不是JSON&#xff0c;JSON也不是JavaScript映射用:隔開并列數據用,隔開映射的集合用{}包裹鍵…

iOS開發經驗總結

在iOS開發中經常需要使用的或不常用的知識點的總結&#xff0c;幾年的收藏和積累&#xff08;踩過的坑&#xff09;。 一、 iPhone Size 二、 給navigation Bar 設置 title 顏色 123UIColor *whiteColor [UIColor whiteColor];NSDictionary *dic [NSDictionary dictionaryWit…

http --- 緩存

Web緩存: // 是可以自動保存常見文檔副本的HTTP設備. // 當Web請求抵達緩存時,如果本地有"已緩存的"副本,就可以從本地存儲設備而不是原始服務器中提取這個文檔.冗余的數據傳輸: // 有很多客戶端訪問一個流行的原始服務器頁面時,服務器會多次傳輸同一份文檔 // 每次…

Django 下添加左側字段顯示和搜索

在對應的apps下建立xadmin.py from .models import EmailVerifyRecord import xadminclass EmailVerifyRecordAdmin(object): list_display [code,email,send_type,send_time]//字段顯示 search_fields [code,email,send_type]//搜索 xadmin.site.register(EmailVerify…

免費分享老男孩全棧9期視頻,共126天

免費分享老男孩全棧9期視頻&#xff0c;共126天。 及時保存避免失效&#xff1a;http://mihon.cn/article/26.html/ 轉載于:https://www.cnblogs.com/mihon/p/9372881.html

ES5 數組擴展方法 forEach/filter/map的使用與重寫

ES3 splice slice join sort &#xff08;IE5、IE6&#xff09; 數組擴展方法 ES5&#xff08;在ES3的基礎上增加、修正&#xff09; forEach 可能會改變原數組(直接操作了arr[i]&#xff0c;沒有使用深拷貝)參數1&#xff1a;回調函數&#xff08;如果不使用箭頭函數&#xf…

http --- 網關、隧道、中繼

網關: // 作為某種翻譯器使用,抽象出了一種能夠到達的資源 // 應用程序可以請求網關來處理某條請求. // 在HTTP和其他協議及其應用程序之間起到接口作用FTP URL的HTTP請求: GET ftp://ftp.irs.gov/pub/00-index.txt HTTP/1.0 Host: ftp.irs.gov User-agent: SuperBrowser 4.2…

狀態碼

服務器向用戶返回的狀態碼和提示信息&#xff0c;常見的有以下一些&#xff08;方括號中是該狀態碼對應的HTTP動詞&#xff09; 200 OK - [GET]&#xff1a;服務器成功返回用戶請求的數據&#xff0c;該操作是冪等的&#xff08;Idempotent&#xff09;。 201 CREATED - [POST/…

187. Repeated DNA Sequences重復的DNA子串序列

&#xff3b;抄題&#xff3d;&#xff1a; All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a fun…

http --- cookie與會話跟蹤

以購物網站Amazon.com為例 // (a)客戶端:首次請求Amazon.com根頁面 GET / HTTP/1.0 Host: www.amazon.com// (b)服務器:將客戶端重定向到一個電子商務軟件的URL上 HTTP/1.1 302 Found Location: http://www.amazon.com:80/exec/obidos/subst/home/redirect.html// (c)客戶端:對…

ES5 every/some/reduce/reduceRight的使用與重寫

被作為實參傳入另一函數&#xff0c;并在該外部函數內被調用&#xff0c;用以來完成某些任務的函數&#xff0c;稱為回調函數。 break、return用于終止循環的區別&#xff1a; return只能用在函數體內&#xff08;單獨一個for循環里直接用return會報錯&#xff09;對于多層循環…

同步異步單線程多線程初級理解

對于我開始接觸同步異步單線程多線程的概念的時候&#xff0c;都是分別理解同步和異步、單線程和多線程概念&#xff0c;當看到“使用同步方法保證線程安全”時愚昧的理解為那就是單線程咯&#xff1b;于是就陷入了困惑&#xff0c;同步等于單線程嗎&#xff1f;下面是我自己不…

http --- 基本認證與摘要認證

基本認證: // (a)客戶端:查詢 GET /cgi-bin/checkout?cart17854 HTTP/1.1// (b)服務器:質詢 HTTP/1.1 401 Unauthorized WWW-Authenticate: Basic realm"Shopping Cart"// (c)客戶端:響應 GET /cgi-bin/checkout?cart17854 HTTP/1.1 Authorization: Basic YnJpYW4…

對棧

1331【例1-2】后綴表達式的值 #include<bits/stdc.h>using namespace std;int sta[101];char s[256]; int comp(char s[256]){ int i0,top0,x; while(i<strlen(s)-2) { switch(s[i]) { case:sta[--top]sta[top1];break; case-:sta[--top]-sta[top1];break; case*:sta[…