Jshint / PhpStorm: “Unresolved variable” when using jquery .data()

独自空忆成欢 提交于 2019-12-07 20:11:21

问题


Phpstorm keeps telling me I have an undefined variable input.connectto

Html: <div class="b-showColorinList" data-connectto="123456" data-othervalue="Lorem Ipsum">...

JS:

$(document).on('click', '.b-showColorinList', function() {
    cm.showColorInList( $(this) );
});

And:

/**
 * Uses ajax to get other color in list view
 * @param {object} inputObj
 */
cm.showColorInList = function(inputObj) {
"use strict";

var input = inputObj.data(),
    parent = $("#"+input.connectto),
    othervalue = input.othervalue;

I know I can ignore a line in jshint, but is there any way to make it correct with jsdoc, example define input as an object


回答1:


Accordingly to JSDoc docs the proper way should using @typedef to define actual object structure (especially useful if it will be re-used later in another place) and @type to declare type of particular variable:

/**
 * @typedef {Object} MyInputData
 * @property {string} connectto 
 * @property {string} othervalue
 */

/** @type {MyInputData} */
var input = inputObj.data();

This one (with just @typedef and variable name as type name) seems to work in PhpStorm as well:

/**
 * @typedef {Object} input
 * @property {string} connectto 
 * @property {string} othervalue
 */
var input = inputObj.data();


来源:https://stackoverflow.com/questions/32993268/jshint-phpstorm-unresolved-variable-when-using-jquery-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!