本文介绍了Jshint/PhpStorm:“未解析的变量"当使用jquery .data()时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Phpstorm不断告诉我我有一个未定义的变量input.connectto

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

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

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

JS:

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

并且:

/**
 * 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;

我知道我可以忽略jshint中的一行,但是有什么方法可以用jsdoc使其正确,例如将input定义为对象

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

推荐答案

根据 JSDoc文档正确的方法应该使用@typedef定义实际的对象结构(特别是有用的,如果以后将在另一个地方重用它),并且使用@type声明特定变量的类型:

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();

这个(只有@typedef并且变量名作为类型名)似乎也可以在PhpStorm中使用:

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();

这篇关于Jshint/PhpStorm:“未解析的变量"当使用jquery .data()时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:50