【问题标题】:Unused JavaScript arguments未使用的 JavaScript 参数
【发布时间】:2020-08-18 02:23:09
【问题描述】:

我是 JavaScript 新手。我想理解 JavaScript 中的一个特性。请考虑以下示例:

function Dog(name) {
  this.name = name
}
Dog.prototype.bark = function() {
  console.log(this.name +" :Hello, human.");
}
var max = new Dog("Max", "Buddy")
max.bark();

如您所见,在创建新狗对象时传递了两个参数,而只需要一个(名称)。 我知道第二个参数未使用,但它存储在哪里/是否有任何实用程序可以传递额外的参数?

提前谢谢你!

【问题讨论】:

  • 如果您没有使用“严格模式”,那么您可以访问函数内部arguments数组中传递给函数的所有参数。
  • 谢谢,伙计们。那么 JS 如何读取我创建新 Dog 对象 max 的行——特别是当它读取“Buddy”时它会做什么以及它对它做了什么,或者 JS 甚至不会读取它?

标签: javascript function javascript-objects


【解决方案1】:

可以在arguments object中检索:

arguments 是一个类似数组的对象,可以在函数内部访问 包含传递给该函数的参数的值。

function Dog(name) {
  console.log(arguments);
  this.name = name;
}
Dog.prototype.bark = function() {
  console.log(this.name +" :Hello, human.");
}
var max = new Dog("Max", "Buddy");
max.bark();

现代替代方案是spread operator

function Dog(...names) {
  console.log(names);
}
var max = new Dog("Max", "Buddy");

【讨论】:

    【解决方案2】:

    它们存储在arguments 数组中:

    function Dog(name) {
      this.name = name
      console.log(arguments)
    }
    Dog.prototype.bark = function() {
      console.log(this.name +" :Hello, human.");
    }
    var max = new Dog("Max", "Buddy", "Arm", {name: "Jacob"})
    max.bark();

    【讨论】:

      【解决方案3】:

      如果你不是在严格模式下使用 js,你可以访问函数的 arguments 属性。

      function Dog(name) {
        this.name = name
        // all the arguments are inside arguments property of function
        console.log(arguments);
      }
      Dog.prototype.bark = function() {
        console.log(this.name +" :Hello, human.");
      }
      var max = new Dog("Max", "Buddy")
      max.bark();

      【讨论】:

        【解决方案4】:

        Javascript 是一种动态语言。相同的函数可以接受动态参数。有多种方法可以访问通过 2 个函数/构造函数传递的所有变量。

        1. 使用arguments
        2. 使用可变参数或rest operator
        3. rest 运算符和参数的混合。

        这取决于您希望如何使用这些值。请参阅以下示例。

        function DogArgs(name) {
          this.name = name;
          console.log(arguments); // array like structure [Arguments] { '0': 'Max', '1': 'Buddy' }
        }
        
        function DogRest(...names) {
          this.name = names[0];
          console.log(names); // Array [ 'Max', 'Buddy' ]
        }
        function DogPartialRest(name, ...others) {
          this.name = name;
          console.log(others); // Array [ 'Buddy' ]
        }
        
        var max1 = new DogArgs("Max", "Buddy");
        var max2 = new DogRest("Max", "Buddy");
        var max3 = new DogPartialRest("Max", "Buddy");

        示例 1:使用参数

        function sum() {
          const values = Array.from(arguments); // convertin array, since arguments is not array
          return values.reduce((sum, val) => (sum += val), 0);
        }
        console.log(sum(1, 2, 3)); // 6
        console.log(sum(1, 2, 3, 4)); // 10
        

        示例 2:使用 vaargs 或 rest 运算符。

        function sum(...values) {
          return values.reduce((sum, val) => (sum += val), 0);
        }
        console.log(sum(1, 2, 3)); // 6
        console.log(sum(1, 2, 3, 4)); // 10
        

        示例 3:rest 运算符和参数的混合

        function Person(name, age, ...rest) {
          this.name = name
          this.age = age
          this.salary = 0
          if(rest.length) {
            this.salary = rest[0]
            this.address = rest[1] || ''
          }
        }
        console.log(new Person("deepak", 30)) // Person { name: 'deepak', age: 30, salary: 0 }
        console.log(new Person("deepak", 30, 2000)) // Person { name: 'deepak', age: 30, salary: 2000, address: '' }
        

        【讨论】:

          猜你喜欢
          • 2018-03-23
          • 2014-12-23
          • 2011-09-20
          • 2022-08-09
          • 1970-01-01
          • 2010-09-29
          • 1970-01-01
          • 1970-01-01
          • 2018-01-14
          相关资源
          最近更新 更多