关于参数的困惑

时间:2019-01-22 14:28:35

标签: javascript

var Type = {};

for (var i = 0, type; type = ['String', 'Array', 'Number'][i++];) {
  (function(type) {
    Type['is' + type] = function(obj) {
      console.log(obj)
      return Object.prototype.toString.call(obj) === '[object ' + type + ']';
    }
  })(type);
}

console.log(Type.isArray([]))

console.log(Type.isString('str'))

非常困惑为什么'obj'等于'type'

1 个答案:

答案 0 :(得分:0)

查看代码中的注释

var Type = {};

// This is like 
// types = ['String', 'Array', 'Number'];
// for (var i = 0; i < types.lenght; i++) {
//   var type = types[i];
//   // ...
// }
// type = undefined;
for (var i = 0, type; type = ['String', 'Array', 'Number'][i++];) {

  // This is like
  // var closure = function(type) {
  //   // ...
  // };
  // closure(type)
  (function(type) {

    // The global object 'Type' is getting a new element which is a function.
    // This element has a name which consists of the prefix 'is'
    // and the value of the varaible 'type'
    // ('String', 'Array', or 'Number')
    // So after the for loop Type will have functions 'isString', 'isArray' ,and 'isNumber'
    // 'obj' will store the parameter that the function is called with
    // for example: Type.isArray([1,2,3]) will store [1,2,3] with the variable obj 
    Type['is' + type] = function(obj) {
      // Since this is a function inside of a function,
      // the variable 'type' is known inside this (inner) function 
      // with the value it has at the time the outer was called
      // (@see JavaScript closures)
      console.log('type', type, 'obj', obj)
      // This is a little trick. The `toString()` method of arrays, strings, and numbers outputs the contents of these objects. The same method of an object returns '[object Object]'
      // Here we bend that `toString` method of the object 'class' to be called from within 
      // our Array, String, or Number and get what we need
      //
      // Why don't just use `typeof` operator? Because `typeof [] === 'object'`
      //
      // This will return true, if the String representation of 'obj'
      // fits type
      return Object.prototype.toString.call(obj) === '[object ' + type + ']';
    }
  })(type);
  
  // This was called three times
  // Type has now three new elements: 'isString', 'isArray', 'isNumber', all being functions
}

console.log(Type.isArray([]))

console.log(Type.isString('str'))

console.log(typeof [])

附录

您可能已经在对象创建和访问的语法上遇到了麻烦,因为有两种方法可以访问分配给对象键的值

// They all overwrite the value of assigned to the key `myKey`
var key='Key';
myObj = {myKey: 'literal object initializer', unchanged: 'This value will remain unchanged in this example'};
console.log(myObj)

myObj.myKey = 'object access operator; dot notation';
console.log(myObj)

myObj['myKey'] = 'object access operator; brackets notation';
console.log(myObj)

myObj['my' + 'Key'] = 'object access operator; brackets notation with string concatenation';
console.log(myObj)

myObj['my' + key] = 'object access operator; brackets notation with string concatenation and variable interpolation';
console.log(myObj)

请参阅Objects and Properties

上的MDM文档