JSDoc Passing Array of Objects Notation

Questions, tips and tricks and techniques for scripting in Servoy

JSDoc Passing Array of Objects Notation

Postby kwpsd » Mon Feb 25, 2013 11:41 pm

Version: 6.1.3 - build 1424

How do you JSDoc annotate a method whose only parameter is an array of Objects?

I use the following JSDoc line:

Code: Select all
@param {Array<Object>} a An array of Objects


which seems to satisfy the JSDoc parser, but when referencing Object members, the following warning is emitted:

The property member_name is undefined for the Javascript type Object.


Meta-code that emits the error:

Code: Select all
for ( var i = 0; i < a.length; i++ )
{
    var o = a[ i ]
       
    var x = o.member_name

    <more code>
}


I tried various permutations of the Object double-braces notation in the @param line of code, but the JSDoc parser complains:

{{ member_1 : String, member_2 : Number, member_3 : Boolean }}


By the way, the method's code works as expected...it's just the JSDoc warnings that I cannot seem to clear.

EDIT: Never mind...I figured it out. Keep the JSDoc @param as is and add @type with the double-braces detail to the declared Object in the code.
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: JSDoc Passing Array of Objects Notation

Postby mboegem » Tue Feb 26, 2013 12:37 am

Hi Kim,

yes, using @type is a solution.

this is what you were looking for:
Code: Select all
@param {Array<{member_name:String, member_id:Number}>} myVarName
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1748
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: JSDoc Passing Array of Objects Notation

Postby kwpsd » Tue Feb 26, 2013 7:49 pm

Thanks for the assist, Marc! I prefer your annotation and changed mine accordingly.
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA


Return to Methods

Who is online

Users browsing this forum: No registered users and 4 guests