草庐IT

const_string

全部标签

javascript - 为什么typeof String返回函数

为什么:console.log(typeofString);当它是object时返回function? 最佳答案 String是字符串对象的构造函数。所有构造函数都是函数,因此您看到的是返回值。您可以通过创建如下代码自己看到:varMyObject=function(value){this.value=value;};MyObject.prototype.getValue=function(){returnthis.value;}console.log(typeof(MyObject));//functionconsole.log(

JavaScript:String.search() 无法搜索 "[]"或 "()"

如果您尝试使用search()函数搜索诸如“[]”或“()”之类的字符串,它不会工作。functionmyFunction(){varstr="Visit[]W3Schools!";varn=str.search("[]");document.getElementById("demo").innerHTML=n;}您可以在-试用W3Schoolshttps://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_search搜索[]返回-1,搜索()返回0。总是。这是为什么? 最佳答案

javascript - React 备忘录功能给出 :- Uncaught Error: Element type is invalid: expected a string but got: object

我有以下功能组件:-importReactfrom'react'import{Dropdown}from'semantic-ui-react'constDropDownMenu=(props)=>{constoptions=[{key:'fruits',text:'fruits',value:'Fruits'},{key:'vegetables',text:'vegetables',value:'Vegetables'},{key:'home-cooked',text:'home-cooked',value:'Home-Cooked'},{key:'green-waste',text:

javascript - 只是好奇如何继承 String 对象。 (原型(prototype))

我知道这是不受欢迎的,我只是在探索这个想法,就我的生活而言,似乎无法按照我想要的方式完成这项工作。这个例子应该解释所有:String.prototype.MyNS=function(){}String.prototype.MyNS.fooify=function(){returnthis+'foo!';}vartheString='Kung';alert(theString.MyNS.fooify());当然,这只是将函数定义附加到“foo”……添加this()是行不通的。我知道我在那里失去了背景,但无法弄清楚如何让原件开火并给我我想要的东西。 最佳答案

javascript - 谷歌图表 : is it possible to use Strings for the x axis?

所以我试着摆弄我的图表,发现整数/float/日期在x轴上工作得很好,但是当你输入一个字符串时,它就爆炸了。我正在尝试绘制a:Word/Count图表,但不幸的是我似乎无法将字符串放在x轴上。这在GoogleCharts中是允许的,还是严格意义上的int/floats/dates? 最佳答案 如下使用vAxis和hAxis属性google.visualization.ColumnChart(document.getElementById('visualization')).draw(data,{title:"YearlyCoffee

javascript - new String() 的行为不像对象那样的数组

varnice=newString("ASH");nice;//String{0:"A",1:"S",2:"H",length:3,[[PrimitiveValue]]:"ASH"}varreverseNice=Array.prototype.reverse.call(nice);reverseNice.toString();//"ASH"而我期望reverseNice是“HSA”。 最佳答案 不能改nice,试试看;nice[0]='f';nice[0];//"A"如果您想使用Array方法,请先将其转换为真正的Arrayvarr

JavaScript element.classList.add ("fa fa-hand-rock-o") 错误 : "String contains an invalid character"

我正在尝试添加一个类使用:document.getElementById("sp1").classList.add("fafa-hand-rock-o");但是显示错误:Stringcontainsaninvalidcharacter 最佳答案 fafa-hand-rock-o不能是单个类,因为类名不能有空格。这里我假设您正在尝试添加两个不同的类。使用classList.add()添加多个类时,将所有类指定为单独的逗号分隔字符串,如:.add("fa","fa-hand-rock-o")代码示例:document.getElemen

javascript - 为什么 String.prototype 里面的 'this' 指的是对象类型,而不是字符串类型?

我正在尝试扩展字符串以提供其自身的散列。我正在使用Node.js加密库。我这样扩展字符串:String.prototype.hashCode=function(){returngetHash(this);};我有一个看起来像这样的getHash函数:functiongetHash(testString){console.log("typeis"+typeof(testString));varcrypto=require('crypto');varhash=crypto.createHash("sha256");hash.update(testString);varresult=hash

javascript - 不能将 String.prototype.match 用作 Array.some 的函数吗?

这行不通:vars='^foo';console.log(['boot','foot'].some(s.match));UncaughtTypeError:String.prototype.matchcalledonnullorundefined但是这样做:vars='^foo';console.log(['boot','foot'].some(function(i){returni.match(s)}));这是为什么?我以某种方式想象String.prototype.match函数太“原始”之类的,但究竟是为什么呢?因为我没有使用ES2015,所以第二个版本看起来很冗长。有替代方案吗

javascript - 为什么我看到 javascript 数组是用 string.split() 创建的?

我在网上看到这样的代码vardays="MondayTuesdayWednesdayThursdayFridaySaturdaySunday".split("");为什么这样做而不是vardays=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];我不认为懒惰或无知与它有任何关系。这是jQuery1.4.2之外的props:"altKeyattrChangeattrNamebubblesbuttoncancelablecharCodeclientXclientYctrlKeycurrentT