Why jshint is not reporting forin (hasOwnProperty) error for the following code? jslint do report error on it but jshint doesn't.
/*jshint forin: true */
(function () {
"use strict";
var obj = {a: 1, b: 2}, i = null;
for (i in obj) {
if (i === 0) {
console.log('blah...');
}
}
}());
Here's the relevant snippet of code from JSHint (modified slightly for formatting):
if (
state.option.forin &&
s &&
(s.length > 1 || typeof s[0] !== "object" || s[0].value !== "if")
) {
warning("W089", this);
}
The important part is s[0].value !== "if". JSHint won't raise an error if the first statement of the for...in body is an if statement, regardless of the condition of that statement.
Related
Because I add a custom AOD clock in the watch file (the code is copied from another open source author, which is an effective code).
But the AOD interface seems to be locked in the original AOD. Did I miss some important settings?
When I set on the normal dial interface, the clock can be displayed in the normal interface, but the AOD clock has not changed. At present, the AOD interface seems to be locked and cannot be modified. However, the official ZEPP development file indicates that AOD display can be set.
https://docs.zepp.com/docs/watchface/api/hmSetting/getScreenType/?_highlight=aod#Screentype-number
Can anyone help me change the AOD clock? Did I miss some important settings? Thank you.
Figure 1: AOD interface, clock is not the style I want to change, it seems to be locked in the official AOD style.
enter image description here
Figure 2: Normal interface. The lowest dotted clock at the bottom is the style I want to change and successfully displayed. But he can only display in the normal interface that the AOD interface has not taken effect.
enter image description here
// aod 显示越少越省电
WatchFace.createWidget(hmUI.widget.IMG_TIME, {
hour_zero: 1,
hour_startX: aodhour_X,
hour_startY: aodhour_Y,
hour_array: aodt_array,
hour_space: 15,
hour_align: hmUI.align.LEFT,
minute_zero: 1,
minute_startX: aodminute_X,
minute_startY: aodminute_Y,
minute_array: aodt_array,
minute_space: 15,
minute_align: hmUI.align.LEFT,
show_level: hmUI.show_level.ONLY_NORMAL | hmUI.show_level.ONAL_AOD,
})
app.js
try {
(() => {
var e = __$$hmAppManager$$__.currentApp;
function t() {
return e.app;
}
const n = {};
function o() {
if ("undefined" != typeof self) return self;
if ("undefined" != typeof window) return window;
if ("undefined" != typeof global) return global;
if ("undefined" != typeof globalThis) return globalThis;
throw new Error("unable to locate global object");
}
t()
? DeviceRuntimeCore.HmUtils.gettextFactory(
n,
t().__globals__.lang,
"en-US"
)
: console.log(n);
let r = o();
r.Logger ||
("undefined" != typeof DeviceRuntimeCore &&
(r.Logger = DeviceRuntimeCore.HmLogger));
let i = o();
i.Buffer ||
("undefined" != typeof Buffer
? (i.Buffer = Buffer)
: (i.Buffer = DeviceRuntimeCore.Buffer));
let u = o();
"undefined" == typeof setTimeout &&
"undefined" != typeof timer &&
((u.clearTimeout = function (e) {
e && timer.stopTimer(e);
}),
(u.setTimeout = function (e, t) {
const n = timer.createTimer(
t || 1,
Number.MAX_SAFE_INTEGER,
function () {
u.clearTimeout(n), e && e();
},
{}
);
return n;
}),
(u.clearImmediate = function (e) {
e && timer.stopTimer(e);
}),
(u.setImmediate = function (e) {
const t = timer.createTimer(
1,
Number.MAX_SAFE_INTEGER,
function () {
u.clearImmediate(t), e && e();
},
{}
);
return t;
}),
(u.clearInterval = function (e) {
e && timer.stopTimer(e);
}),
(u.setInterval = function (e, t) {
return timer.createTimer(
1,
t,
function () {
e && e();
},
{}
);
})),
(e.app = DeviceRuntimeCore.App({
globalData: {},
onCreate(e) { },
onShow(e){},
onHide(e){},
onDestroy(e) { },
onError(e) { },
onPageNotFound(e) { },
onUnhandledRejection(e) { }
})),
(t().__globals__ = {
lang: new DeviceRuntimeCore.HmUtils.Lang(
DeviceRuntimeCore.HmUtils.getLanguage()
),
px: DeviceRuntimeCore.HmUtils.getPx(480)
}),
(t().__globals__.gettext = t()
? DeviceRuntimeCore.HmUtils.gettextFactory(
n,
t().__globals__.lang,
"en-US"
)
: console.log(n));
})();
} catch (e) {
console.log(e);
}
Hi Iam trying to detect if the user is launching my app for the first time with this code. But in the $(document).on('pageshow','#welcome-page', function(){ part, my variable firstlaunch is always undefined. Can you help me figure out what to do ?
Thanks
var firstlaunch;
$(function(firstlaunch) {
firstlaunch = parseInt(window.localStorage.getItem("firstlaunch"));
//store 0 for next time
if ( firstlaunch == null || typeof firstlaunch == 'undefined' || isNaN(firstlaunch) ) {
firstlaunch = 1;
window.localStorage.setItem("firstlaunch", 0);
}
});
$(document).on('pageshow','#welcome-page', function(){
console.log("firstlaunch pageshow #welcome-page = "+firstlaunch);
console.log("typeof firstlaunch pageshow #welcome-page = "+typeof firstlaunch);
if ( firstlaunch ==1 || typeof firstlaunch == 'undefined' || isNaN(firstlaunch) ){
//do things if first visit
} else if (firstlaunch == 0) {
//do things if not first visit
} else {
console.log('Error : firstlaunch ='+firstlaunch);
}
});
The localStorage.getItem() call will return null if it does not exist.
The simplest way to do this would be like this:
$(document).one('pageshow','#welcome-page', function(){
var noRunCount = (localStorage.getItem("runCount") == null);
if (noRunCount) {
// You know it's the first time this is run because
// the variable isn't even defined yet
localStorage.setItem("runCount", 1);
} else {
// The variable exists, so it must have been set by your app
localStorage.setItem("runCount", (localStorage.getItem("runCount") + 1));
}
}
One thing to note here is the use of $(document).one('pageshow', ) instead of $(document).on('pageshow', ). This makes the logic in this block only execute the first time the welcome page is loaded.
Thanks to you all.
I went with the solution of #ezanker because it was close to my code and simple.
Thank you again.
$(document).on("pageshow", "#welcome-page", function(){
alert(IsFirstLaunch());
});
function IsFirstLaunch(){
var fl = window.localStorage.getItem("firstlaunch");
if (fl && parseInt(fl) == 0){
return false;
} else {
window.localStorage.setItem("firstlaunch", "0");
return true;
}
}
I'm using select2 with Bootstrap 3.
Now I would like to know whether it is possible to display all optgroup items if the search matches the optgroup name while still being able to search for items as well. If this is possible, how can I do it?
The above answers don't seem to work out of the box with Select2 4.0 so if you're hunting for that, check this out: https://github.com/select2/select2/issues/3034
(Use the function like this: $("#example").select2({matcher: modelMatcher});)
function modelMatcher (params, data) {
data.parentText = data.parentText || "";
// Always return the object if there is nothing to compare
if ($.trim(params.term) === '') {
return data;
}
// Do a recursive check for options with children
if (data.children && data.children.length > 0) {
// Clone the data object if there are children
// This is required as we modify the object to remove any non-matches
var match = $.extend(true, {}, data);
// Check each child of the option
for (var c = data.children.length - 1; c >= 0; c--) {
var child = data.children[c];
child.parentText += data.parentText + " " + data.text;
var matches = modelMatcher(params, child);
// If there wasn't a match, remove the object in the array
if (matches == null) {
match.children.splice(c, 1);
}
}
// If any children matched, return the new object
if (match.children.length > 0) {
return match;
}
// If there were no matching children, check just the plain object
return modelMatcher(params, match);
}
// If the typed-in term matches the text of this term, or the text from any
// parent term, then it's a match.
var original = (data.parentText + ' ' + data.text).toUpperCase();
var term = params.term.toUpperCase();
// Check if the text contains the term
if (original.indexOf(term) > -1) {
return data;
}
// If it doesn't contain the term, don't return anything
return null;
}
Actually found the solution by modifying the matcher opt
$("#myselect").select2({
matcher: function(term, text, opt){
return text.toUpperCase().indexOf(term.toUpperCase())>=0 || opt.parent("optgroup").attr("label").toUpperCase().indexOf(term.toUpperCase())>=0
}
});
Under the premise that the label attribute has been set in each optgroup.
Found a solution from select2/issues/3034
Tested with select2 v.4
$("select").select2({
matcher(params, data) {
const originalMatcher = $.fn.select2.defaults.defaults.matcher;
const result = originalMatcher(params, data);
if (
result &&
data.children &&
result.children &&
data.children.length
) {
if (
data.children.length !== result.children.length &&
data.text.toLowerCase().includes(params.term.toLowerCase())
) {
result.children = data.children;
}
return result;
}
return null;
},
});
A few minor changes to people suggested code, less repetitive and copes when there are no parent optgroups:
$('select').select2({
matcher: function(term, text, opt){
var matcher = opt.parent('select').select2.defaults.matcher;
return matcher(term, text) || (opt.parent('optgroup').length && matcher(term, opt.parent('optgroup').attr("label")));
}
});
I run the code below and I got an error without any stack trace.
My code:
typedef Check<T>(T value, [onError(T value)]);
main () {
List<Check> checks = [
(str) => str != null,
(str) => !str.isEmpty
];
Check<String> doCheck = (String value, [onError(String)]) {
checks.forEach((Check check) {
if (?onError) {
check(value, onError);
} else {
check(value);
}
});
};
doCheck("10");
}
And, the error I got.
file:///..()../sample.dart': Error: line 11 pos 12: formal parameter name expected
if (?onError) {
I want to get onError as an optional parameter in doCheck function, and pass this parameter to other functions in checks.
I confirmed to forward an optional parameter to 'one' function...
Is this one of restrictions to optional parameters?
I would say it is a bug (see issue 8007). To work around it, you have to use a temporary variable :
typedef Check<T>(T value, [onError(T value)]);
main () {
List<Check> checks = [
(str) => str != null,
(str) => !str.isEmpty
];
Check<String> doCheck = (String value, [onError(String)]) {
final isOnErrorPresent = ?onError;
checks.forEach((Check check) {
if (isOnErrorPresent) {
check(value, onError);
} else {
check(value);
}
});
};
doCheck("10");
}
I am trying to create a plugin for JQuery Mobile. Does anyone have a template or examples to help? Currently, I have the following defined in myplugin.js
(function ($) {
$.fn.myPlugin = function (options) {
var defaults = { e: 0 },
settings = $.extend({}, defaults, options);
var h= $.myPlugin.getHtml(options.e);
alert("here 1");
if ((h != null) && (h != undefined) && (h.length > 0)) {
alert("here 2");
this.html(h);
}
};
$.myPlugin = {
getHtml: function (e) {
var s = "";
return s;
}
};
})(jQuery);
I am trying to initialize an instance of this plugin like such:
$("#pluginInstance", "#myPage").myPlugin({ e: 0 });
Oddly, neither alert dialog appears. There aren't any errors in the console either. What am I doing wrong?
How about this or this?
If you look how JQM is designed, the syntax is pretty similar. I'm also sticking with it, when working on plugins I'm doing.