Adding items to an object through the .push() method

后端 未结 5 428
Happy的楠姐
Happy的楠姐 2020-12-04 08:15

I\'m doing a loop through few input elements of \'checkbox\' type. After that, I\'m adding values and checked attributes to an array. This is my code:

var st         


        
5条回答
  •  悲哀的现实
    2020-12-04 08:38

    .push() is a method of the Built-in Array Object

    It is not related to jQuery in any way.

    You are defining a literal Object with

    // Object
    var stuff = {};
    

    You can define a literal Array like this

    // Array
    var stuff = [];
    

    then

    stuff.push(element);
    

    Arrays actually get their bracket syntax stuff[index] inherited from their parent, the Object. This is why you are able to use it the way you are in your first example.

    This is often used for effortless reflection for dynamically accessing properties

    stuff = {}; // Object
    
    stuff['prop'] = 'value'; // assign property of an 
                             // Object via bracket syntax
    
    stuff.prop === stuff['prop']; // true
    

提交回复
热议问题