首页 > 其他分享 >js ES5 arguments & arguments.callee & this All In One

js ES5 arguments & arguments.callee & this All In One

时间:2022-10-18 20:15:54浏览次数:69  
标签:ES5 console log arguments new Foo callee

js ES5 arguments & arguments.callee & this & ES6 new.target All In One

js ES5 arguments & arguments.callee & this

// ES5 arguments & arguments.callee & this
function test(a, b, c) { 
  console.log(`this =`, this);
  console.log(`arguments =`, arguments);
  console.log(`arguments.callee =`, arguments.callee);
  console.log(`arguments.callee.name =`, arguments.callee.name);
  const args = Array.prototype.slice.call(arguments, 0); 
  console.log(`args =`, args);
  const arr = [...arguments];
  console.log(`arr =`, arr); 
}

test(1, 2, 3, 4);
/*
this = Window {window: Window, self: Window, document: document, name: '', location: Location, …}

arguments = Arguments(4) [1, 2, 3, 4, callee: ƒ, Symbol(Symbol.iterator): ƒ]

arguments.callee = ƒ test(a, b, c) { 
  console.log(`this =`, this);
  console.log(`arguments =`, arguments);
  console.log(`arguments.callee =`, arguments.callee);
  console.log(`arguments.callee.name =`, arguments.call…

arguments.callee.name = test

args = (4) [1, 2, 3, 4]0: 11: 22: 33: 4length: 4[[Prototype]]: Array(0)
arr = (4) [1, 2, 3, 4]

*/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

Array.prototype.slice

// Array.prototype.slice.call(arguments, 0)


const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};

console.log(Array.prototype.slice.call(arrayLike, 1, 3));

// slice() is called with `this` passed as the first argument

const slice = Function.prototype.call.bind(Array.prototype.slice);

// ES5 arguments
function list() {
  return slice(arguments);
}

const list1 = list(1, 2, 3); 
// [1, 2, 3]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#using_slice_to_convert_array-like_objects_to_arrays

ES6 new.target

function Foo() {
  if (!new.target) {
    // throw String
    throw 'Foo() must be called with new';
  }
}

try {
  Foo();
} catch (err) {
  console.log(err);
  // Foo() must be called with new
}
// ES5 限制构造函数的调用方式,必须使用 new 关键字
function Foo() {
  if (!new.target) {
    // throw Error ❌
    throw new Error(`❌ Foo() must be called with the new keyword!`);
  } else {
    console.log(`✅ Foo() called with the new keyword!`);
  }
}

try {
  Foo();
} catch (err) {
  console.log(err);
}
// Error: ❌ Foo() must be called with the new keyword!

try {
  const foo = new Foo();
} catch (err) {
  console.log(err);
}
// ✅ Foo() called with the new keyword!

// ES6 限制 Class  构造函数的调用方式,必须使用 new 关键字
class Foo {
  constructor(name) {
    if (!new.target) {
      // throw Error ❌
      throw new Error(`❌ class Foo must be called with the new keyword!`);
    } else {
      console.log(`✅ class Foo called with the new keyword!`);
    }
    this.name = name ?? 'default name';
  }
}

try {
  Foo();
} catch (err) {
  console.log(err);
  // Foo() must be called with new
}
// ❌ TypeError: Class constructor Foo cannot be invoked without 'new'


try {
  const foo = new Foo(`es6`);
} catch (err) {
  console.log(err);
}
// ✅ class Foo called with the new keyword!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载

标签:ES5,console,log,arguments,new,Foo,callee
From: https://www.cnblogs.com/xgqfrms/p/16803885.html

相关文章

  • [Typescript] 56. Medium - Flip Arguments
    Implementthetypeversionoflodash's _.flip.Type FlipArguments<T> requiresfunctiontype T andreturnsanewfunctiontypewhichhasthesamereturnty......
  • ES5 和 ES6 的区别,说几个 ES6 的新增方法
    ECMAscript5.,即ES5,表示ECMAscript的第五次修订-2009;ECMAscript6.,即ES6,表示ECMAscript的第六次修订-2015;ES6是对于ES5的一次改进,更加简洁,提高了开发效率;1.新......
  • ORA-00600: internal error code, arguments: [4:kgstmLdiToEpochTs]
    客户备库异常掉电,启动报错ORA-00600:internalerrorcode,arguments:[4:kgstmLdiToEpochTs]通过MOS查询找到官方文档 (DocID1334956.1) 实际数据:   ......
  • arguments详解,类数组转数组方法
    为什么需要arguments对象由于​​JavaScript​​​允许函数有不定数目的参数,所以需要一种机制,可以在函数体内部读取所有参数。这就是​​arguments​​对象的由来。通......
  • es5 JSON对象
    es5JSON对象1.JSON.stringify(obj/arr)js对象(数组)转换为json对象(数组)2.JSON.parse(json)json对象(数组)转换为js对象(数组)<!DOCTYPEhtml><htmllang="en"......
  • 【前端】【ES5】【JS的基本使用】
    【前端】【ES5】【JS的基本使用】一、前概 首先我们需要明白js是一门解释型(解释一行执行一行)面向对象的编程语言,在这个前提下,我们再继续下面的文档阅读。 面向对象是一......
  • ...args剩余参数和 arguments对象的区别
    一、...args剩余参数(展开运算符)允许一个表达式在某处展开。展开运算法 在 多个参数(函数调用)、多个元素(用于数组和字面量)和多个变量(用于解构赋值) 地方使用。剩余参数语......
  • 复习——高级语法对象原型,es5新增语法
    今天的开始进入了js的高级语法我马上也要复习完了,之前学到闭包递归,就回去复习去了,复都复习这么久而且,复习的过程真的比学知识的过程难熬的多,只不过终于要复习完了,再来点es6......
  • Typescripe类型体操 - FlipArguments
    题目中文实现lodash中_.flip方法的类型版本FlipArguments<T>类型接收泛型参数T并返回一个函数类型,且此函数类型有和T相同的返回类型但其参数的顺序是倒过来的E......
  • ES6 关键字 let 和 ES5 及以前关键字 var 的区别
    var在ES5及以前,通过var在块级作用域中声明的变量,外边也可以访问。块级作用域就是一对{}的作用域;块级作用域可以是控制语句的作用域,也就是非函数的作用域。functionf()......