将带有函数的Javascript对象转换为字符串(Converting a Javascript Object with Functions into a String)
我今天遇到了一个情况,我需要通过HTTP访问一个Javascript文件。 实际程序与NodeJS一起运行,位于/ home /目录之外。
我试图访问javascript文件的原因是它有一个我经常使用的函数列表,并希望将它们与PhantomJS一起使用(基本上将它注入不同的网站)。
我能够通过将整个节点移动到一个可访问的文件夹并让htaccess拒绝访问除1个文件之外的所有内容来使其工作,但我很好奇是否有人会知道:
是否可以获取整个对象(函数和预设值)并将它们输出为字符串(就像导航到file.js页面一样)。 这样我就可以创建一个HTTP页面,发送正确的头文件,并基本上创建一个可访问的JS页面。
我环顾四周,但无法找到任何关于实际逐字输出对象的信息。
I came across a situation today where I needed a single Javascript file to be accessible through HTTP. The actual program was running with NodeJS and was outside of the /home/ directory.
The reason I was trying to access the javascript file was it has a list of functions that I use often, and wanted to utilize them with PhantomJS (essentially injecting it into a different website).
I was able to get it to work by moving the entire node into an accessible folder and having htaccess deny access to everything except 1 file, but I am curious if anyone would know:
Would it be possible to take an entire object (functions and preset values) and output them as a string (same as if you navigated to a file.js page). This way I could create an HTTP page, send the proper headers and essentially create an accessible JS page.
I looked around but couldn't find any information on actually outputting a object verbatim.
最满意答案
将用户Houshalter的答案检查到将对象转换为字符串的问题 。
所有答案提供了如何将没有函数的对象转换为字符串的不同示例 但他解释了如何使用包含函数的对象来做到这一点。
Check this answer from user Houshalter to the question Converting an object to a string.
All answers provide different examples how to convert object without function to string. But he explains how to do that with object which contains also functions.
相关问答
更多-
TCP/IP模型是一个________。[2022-12-11]
a -
将javascript字符串转换为html对象[复制](converting a javascript string to a html object [duplicate])[2022-07-05]
你不能只用方法,除非你使用一些JavaScript框架,如支持它的jQuery。 string s = '' var htmlObject = $(s); // jquery call 但仍然不会被getElementById找到,因为要工作,元素必须位于DOM中......只是在内存中创建不会将其插入到DOM中。 你需要使用append或appendTo或after等..把它放在dom第一.. Of'course所有这些都可以通过正常的JavaScript来完 ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如果删除周围的括号,您将获得一个JSON字符串,可以使用JSON.parse()将其转换为对象: var s = '({"test1":[{"test2":55,"test":"15.06"}, {"test3":55,"test4":"15.08"}]})', j = s.replace(/^\((.+)\)$/, '$1'), //remove surrounding parentheses o = JSON.parse(j); console.log(o); If you ...
-
如何将带有函数的javascript数据转储到eval-able字符串?(How to dump javascript data with functions to eval-able string?)[2023-02-20]
JSON不能代表函数。 没有办法把你的数组放在JSON中。 人们经常使用术语“JSON”来表示任何Javascript对象文字。 它不是:它是严格定义的数据格式。 更新:OP已从标题中删除“JSON”。 I found what satisfies me: jsDump -
由于您在直接将字符串转换为日期的同时获得了NaN 。 您可以将字符串拆分为空格, -和:然后将该值传递给日期构造函数并生成日期对象。 const str = `2018-04-28 09:00:00`; const [date, time] = str.split(' '); const [year, month, day] = date.split('-'); const [hh, mm, sec] = time.split(':'); const dateObj = new Date(year, ...
-
如果你只想要一个简单地调用方法的函数数组,那就简单多了: for(var i=0; i将用户Houshalter的答案检查到将对象转换为字符串的问题 。 所有答案提供了如何将没有函数的对象转换为字符串的不同示例 但他解释了如何使用包含函数的对象来做到这一点。 Check this answer from user Houshalter to the question Converting an object to a string. All answers provide different examples how to convert object without function to ...
将包含任何内容的javascript对象转换为字符串(Converting javascript object which contains nothing to a string)[2022-12-14]
我发现如果我做了以下事情,我可以防止崩溃 let [success, out, err, errno] = GLib.spawn_sync(null, ["sqlite3", "-line", places, sqlQuery], null, 4, null); if (out.length > 0) { let records = out.toString().split('\n\n'); } 我不明白为什么out.toString()不能给我一个空字符串而不是崩溃。 希望我能够了解更多 ...Javascript将YYMMDD格式的字符串转换为日期对象(Javascript converting YYMMDD formatted string into date object)[2022-06-18]
你正在做的很好,编写简单的代码来完成常见任务没有什么大问题。 如果您发现自己执行了大量的日期计算,则momentjs是一个非常小的(5kb)库,可以像这样处理日期。 例如,您需要的内容可以写成: moment("20130914", "YYYYMMDD"); What you're doing is fine, there is no big problem in writing simple code to do common tasks. If you find yourself performing ...相关文章
更多- Object Oriented Programming
- Java String类
- 怎么得到String数组?
- javascript 问题
- Solr: a custom Search RequestHandler
- Spring Data: a new perspective of data operations
- Drupal Forums instead of phpBB or vBulletin: A casestudy
- Guava学习笔记:复写的Object常用方法
- javascript常用对象
- redis 字符串(String) SET 操作
最新问答
更多- 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
- 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
- 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
- ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
- Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
- 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
- 功能包装避免重复(wrap of functions avoid duplicating)
- Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
- 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
- VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
- 大学电脑四级对初学者来说要多久能过
- 特殊字符删除?(Special characters remove?)
- Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
- 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
- Javaweb开发,技术路线是什么?该怎么写?
- JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
- 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
- Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
- 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
- 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
- “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
- maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
- 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
- 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
- java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
- 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
- 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
- 嵌套的模板可能性(Nested template possibilities)
- 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
- 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)