array + charAt issue

The following code output a k[i].charAt is not a function error. The strange thing is that there is no error - and the result is correct - if instead of i I put a number k[1].charAt(0) . Same thing with indexOf . for (i = 0; n < arguments.length; i++) { k[i] = arguments[i]; if (k[i].charAt(0) == "["){ k[i] = eval(k[i]); } } This code is kind of ambiguous. arguments re

数组+ charAt问题

以下代码输出k[i].charAt is not a function错误。 奇怪的是没有错误 - 并且结果是正确的 - 如果不是i我把一个数字k[1].charAt(0) 。 与indexOf同样的事情。 for (i = 0; n < arguments.length; i++) { k[i] = arguments[i]; if (k[i].charAt(0) == "["){ k[i] = eval(k[i]); } } 这段代码有点不明确。 arguments表示作为类似数组的对象提供给函数的所有参数。 charAt是一个在String.prototype定

isNan() function in Javascript not identifying toString()

The below code is just to display "true" or "false" based on whether the input to isNaN() function is a number or not. In isNaN() function, I am converting the type of number to string using the toString() function. But still, the output I am getting is 'false' instead of 'true' <html> <head> <title>check</title> &

Javascript中的isNan()函数不能识别toString()

下面的代码只是根据isNaN()函数的输入是否是数字来显示“true”或“false”。 在isNaN()函数中,我使用toString()函数将数字类型转换为字符串。 但是,我得到的结果是'假'而不是'真' <html> <head> <title>check</title> <script type='text/javascript'> function checkRun(){ var obj = { atr

Only allow certain types for properties

function Dude() { this.firstName = ''; this.lastName = ''; }; var d = new Dude(); d.firstName = 'Ok mr'; d.firstName = 100; How do I prevent / guard the assignment of 100 to firstName so that it will always be a string? Assigning a number, array another object should convert to a string. Try to make the variables private and write getters and setters for them. In the setter you can c

只允许某些类型的属性

function Dude() { this.firstName = ''; this.lastName = ''; }; var d = new Dude(); d.firstName = 'Ok mr'; d.firstName = 100; 我如何防止/防范100给firstName的赋值,以便它始终是一个字符串? 分配一个数字,数组另一个对象应该转换为一个字符串。 尽量让这些变量保密,并为它们写getter和setter。 在setter中,您可以检查正确的类型。 看到这个答案检查类型。 function Dude() { var firstName = '';

redirect knockout js observable if containing string

I have a input field in my html which basically sends the amount to an observable and then redirects to different pages depending on the amount, but I want to make sure that even if a person who doesnt write amount and types in "string values" should still be able to get redirected, but I am not sure how to accomplish that. HTML CODE <input id="amount" type="text" data-bind="valu

如果包含字符串,则重定向knockout js可观察

我在我的html中有一个输入字段,它基本上把数量发送给一个observable,然后根据金额重定向到不同的页面,但是我想确保即使没有在“字符串值”中写入数量和类型的人应该仍然能够重定向,但我不知道如何完成。 HTML代码 <input id="amount" type="text" data-bind="value : amount" /> <button class="btn button2" type="submit" data-bind=" valueUpdate:'afterkeydown' , click: $root.borrow_first_pageview" >&

javascript function error while adding blank functions

I am relatively new to javascript. following is my .js -file which I am importing in my html file- function primaerror(strmessage) { alert(strmessage); return null; } function showdiagnosis(string pid){} function showimages(){} function showimage(){} I get : Error = "Expected ')'. Line:5 Char:31" Please help me where the error could be? Thanks Javascript doesn't have the keyword s

在添加空白函数时出现javascript函数错误

我相对较新的JavaScript。 以下是我在我的html文件中导入的.js文件 - function primaerror(strmessage) { alert(strmessage); return null; } function showdiagnosis(string pid){} function showimages(){} function showimage(){} 我得到: Error = "Expected ')'. Line:5 Char:31" 请帮助我哪里的错误可能是? 谢谢 Javascript没有keyword string 。 您不应该为变量提供数据类型,甚至可能在形式参数之前不使

Do any standard JavaScript APIs or libraries return strings as String objects?

Until recently, I didn't realise that there are two types of strings (and bools, and numbers) in JavaScript: primitives such as "blah" , and objects such as new String("blah") . They differ in very "gotcha"-prone ways, the biggest one of which seems to be the different typeof value ( "string" vs "object" ), but a number of other differences

是否有任何标准JavaScript API或库以字符串对象的形式返回字符串?

直到最近,我还没有意识到JavaScript中有两种类型的字符串(以及bools和数字):诸如"blah"基元和诸如new String("blah") 。 他们的不同之处非常“疑难杂症”易发的方式,其中最大的一个似乎是不同typeof值( "string" VS "object" ),但其他一些存在差异,一些在MDN记录。 创建String对象没有意义,因为原始字符串的工作原理也一样,JSHint甚至将其视为错误。 所以我真的很想假装S

Easier way to have RegExp.test() return false for null values?

let regex = /[a-z]+/; regex.test('a'); // true regex.test(''); // false regex.test(null); // true regex.test(undefined); // true So based on this link, Is it a bug in Ecmascript - /S/.test(null) returns true?, it looks like the null value is coerced to a string 'null'. WTF? Why on earth is this designed this way? I also can't find any documentation on this behavior. Is there a way

更容易的方法使RegExp.test()对于空值返回false?

let regex = /[a-z]+/; regex.test('a'); // true regex.test(''); // false regex.test(null); // true regex.test(undefined); // true 那么基于这个链接,它是Ecmascript中的一个错误 - /S/.test(null)是否返回true?,它看起来像空值被强制为一个字符串'null'。 WTF? 这是为什么这样设计的? 我也找不到有关此行为的任何文档。 有没有办法返回false的空/未定义的值(没有检查“空”硬编码等)? RegExp.test(

Can Ramda have isString?

Background I am trying to use ramda and I need a pure function that lets me know if a given input is a string or not, much like lodash _.isString . Question After searching everywhere I couldn't find anything in Ramda for this. So I wonder, is there a way, using any of Ramda's existing functions, that I can create a isString function? I find this horribly limiting and is it is no

拉姆达可以有isString吗?

背景 我试图使用ramda,我需要一个纯函数,让我知道给定的输入是否是字符串,很像lodash _.isString 。 题 到处搜寻后,我无法在此找到任何Ramda。 所以我想知道,有没有办法,使用任何Ramda的现有函数,我可以创建一个isString函数? 我发现这种可怕的限制,是不可能的,我可能只是最后使用lodash:S 而不是有isString , isObject , isArray , isFunction等,Ramda只是提供is ,你可以用它来创建任何一种你喜欢:

Easy way to check if a variable is a string?

This question is a spin-off of [] is an instance of Array but "" isn't of String Given that "" instanceof String; /* false */ String() instanceof String; /* false */ new String() instanceof String; /* true */ and typeof "" === "string"; /* true */ typeof String() === "string"; /* true */ typeof new String() === "string"; /* false */ Then, if I have a variable abc and I want to

简单的方法来检查一个变量是否是一个字符串?

这个问题是分拆的[]是数组的一个实例,但是“”不是字符串 鉴于 "" instanceof String; /* false */ String() instanceof String; /* false */ new String() instanceof String; /* true */ 和 typeof "" === "string"; /* true */ typeof String() === "string"; /* true */ typeof new String() === "string"; /* false */ 然后,如果我有一个变量abc ,我想知道它是否是一个字符串,我可以 if(typeof abc === "string" ||

check if an object is string in Javascript

I was following a tutorial that suggested to check if an object is string and not empty as the following: var s = "text here"; if ( s && s.charAt && s.charAt(0)) it is said that if s is string then it has a method charAt and then the last component will check if the string is empty or not. I tried to test it with the other available methods like ( typeof and instanceof ) using

检查一个对象是否是Javascript中的字符串

我正在关注一个教程,该教程建议检查一个对象是否是字符串而不是空的,如下所示: var s = "text here"; if ( s && s.charAt && s.charAt(0)) 据说如果s是字符串,那么它有一个方法charAt,然后最后一个组件将检查字符串是否为空。 我试图用其他可用的方法( typeof和instanceof )来测试它,使用一些SO问题,在这里和这里也是如此! 所以我决定在Js Bin中测试它:jsbin代码如下: var string1 = "text he