Javascript 属性访问速度:var.property 与 var[“property"] 的区别? [英] Javascript property access speed: difference var.property vs. var["property"]?

查看:59
本文介绍了Javascript 属性访问速度:var.property 与 var[“property"] 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的 JavaScript 问题.

I have a very basic JavaScript question.

我正在编写一个将生成 JavaScript 代码的程序.要访问变量的属性,我有两种选择:

I am writing a program which will generate JavaScript code. for accessing a property of a variable i have two choices:

1. make the property access a static query. i.e.
var result = object.property

OR

2. make the property access a dynamic query, i.e.
var result = object["property"]

它对我的不同之处在于,对于第一种情况(静态查询情况),我必须为每个属性访问生成单独的代码.而在第二种情况(动态查询情况)中,我可以为每个属性重用相同的函数.

The difference it makes to me is that for the first case (static query case) i will have to generate separate code for each property access. whereas in the second case (dynamic query case) i can reuse the same function for every property.

我可以决定我是否知道这对性能有什么影响?

I can decide if i know does this makes any difference in performance?

obj.property更快还是obj["property"]?

可能这也取决于将用于解释 javascript 的引擎,所以我必须提到我将使用 Rhino 作为我的 javascript 引擎.

May be this also depends on the engine which will be used to interpret javascript, so i must mention that I will be using Rhino as my javascript engine.

所以请对这个问题有所了解.

So please throw some light on this issue.

谢谢,问候,虚拟

推荐答案

Javascript 中没有静态属性,只有动态属性访问存在.

There are no static properties in Javascript, only dynamic property accessing exists.

无论您在源代码文件中使用什么语法,属性总是以相同的方式查询.

Properties are always queried in the same way regardless of what syntax you put in your source code file.

使用 jshint 为您的 JS 文件推荐好的源代码约定:

Use jshint to recommend good source code conventions for your JS files:

http://jshint.com/

始终建议使用点表示法.仅当您的 Javascript 属性没有以 JS 语法传递的 id 时才使用引号表示法.

Dot notation is always recommended. Use quotation mark notation only if your Javascript property has not id which passes in JS syntax.

这篇关于Javascript 属性访问速度:var.property 与 var[“property"] 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆