“内部”是指那些在 ES5 8.6.2 中定义的:
http://www.ecma-international.org/publications/standards/Ecma-262.htm
可以通过使用访问[[Class]]内部属性
Object.prototype.toString(Object)
这些属性的用途是什么?它们是可访问的吗?
该规范并未声称定义了一种修改方法(p32-footer)。
NOTE This specification defines no ECMAScript language operators or built-in functions that permit a program to modify an object‘s [[Class]] or [[Prototype]] internal properties or to change the value of [[Extensible]] from false to true. Implementation specific extensions that modify [[Class]], [[Prototype]] or [[Extensible]] must not violate the invariants defined in the preceding paragraph.
最佳答案
Are they accessible?
不完全是,您可以弄清楚它们返回什么(根据它们各自的定义 §8.12 ),但您无法改变它们的工作方式。
以下是解决其中大部分问题的方法(在 §8.6.2 中列出)。
对于所有示例,我假设对象存储为 obj,属性键为 name,val 为值,descriptor 是一个属性描述符。
[[原型(prototype)]],Object.getPrototypeOf(obj) [[Class]], Object.prototype.toString.call(obj) [[Extensible]], Object.isExtensible(obj) [[Get]], obj[name][[GetOwnProperty]], Object.getOwnPropertyDescriptor(obj, name) [[GetProperty]],Object.getOwnPropertyDescriptor(obj, name)结合上行继承(Object.getPrototypeOf)[[Put]], obj[name] = val[[CanPut]],查找 getOwnPropertyDescriptor,如果 undefined 是 obj 可扩展的(Object.isExtensible )?否则,检查 可写 或 setter 是否存在 set[[HasProperty]], name in obj [[Delete]], delete obj.name [[DefaultValue]],不确定这个[[DefineOwnProperty]], Object.defineProperty(obj, name, descriptor) What are these properties for?
它们与 JavaScript 引擎应如何根据规范 (§8.12) 工作的内部机制有关,并在算法 (example) 中引用。
在 ES6 中我们可以访问 Proxy s,这意味着我们可以创建对象,然后用 Proxy 包装它们,让我们以自定义方式处理 get、set、has 等
// have some object
let o = {};
// wrap with proxy defining a get handler and set handler
let p = new Proxy(o, {
get(t, n) {
console.log(t, n, t[n]);
return t[n];
},
set(t, n, v) {
t[n] = +v;
return true;
}
});
// now accessing via proxy
p.foo; // undefined
// get handler logs Object o, "foo", undefined (this happens before .foo returns)
p.foo = '123'; // uses handler to sets on `o`
p.foo; // 123, notice value is Number due to set handler
// get handler logs Object o, "foo", 123 (this happens before .foo returns)
关于javascript - 是否可以访问 ES 5-8.6.2 中定义的 "internal"对象属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14369317/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html