变量的自引用在JavaScript中是坏事吗?

时间:2016-02-12 04:40:22

标签: javascript

请考虑以下代码:


compose.service.build_container_labels: Added config hash: 4360ff02d2c79d27ddef844b2823c0f563bfa8ac13c127b2186fbbb5f7b1db7e
compose.cli.verbose_proxy.proxy_callable: docker create_host_config <- (memswap_limit=None, links=[], devices=None, pid_mode=None, log_config={'Type': u'', 'Config': {}}, cpu_quota=None, read_only=None, dns=None, volumes_from=[], port_bindings={}, security_opt=None, extra_hosts=None, cgroup_parent=None, network_mode=u'dockerkibana_default', cap_add=None, restart_policy=None, dns_search=None, privileged=False, binds=[u'/home/ariful/docker-kibana/kibana/config/kibana.yml:/opt/kibana/config/kibana.yml:rw'], ipc_mode=None, mem_limit=None, cap_drop=None, ulimits=None)
compose.cli.verbose_proxy.proxy_callable: docker create_host_config -> {'Binds': [u'/home/ariful/docker-kibana/kibana/config/kibana.yml:/opt/kibana/config/kibana.yml:rw'],
 'Links': [],
 'LogConfig': {'Config': {}, 'Type': u''},
 'NetworkMode': u'dockerkibana_default',
 'PortBindings': {},
 'VolumesFrom': []}
compose.cli.verbose_proxy.proxy_callable: docker create_container <- (name=u'dockerkibana_kibana_1', image=u'dockerkibana_kibana', labels={u'com.docker.compose.service': u'kibana', u'com.docker.compose.project': u'dockerkibana', u'com.docker.compose.config-hash': '4360ff02d2c79d27ddef844b2823c0f563bfa8ac13c127b2186fbbb5f7b1db7e', u'com.docker.compose.version': u'1.6.0', u'com.docker.compose.oneoff': u'False', u'com.docker.compose.container-number': '1'}, host_config={'NetworkMode': u'dockerkibana_default', 'Links': [], 'PortBindings': {}, 'Binds': [u'/home/ariful/docker-kibana/kibana/config/kibana.yml:/opt/kibana/config/kibana.yml:rw'], 'LogConfig': {'Type': u'', 'Config': {}}, 'VolumesFrom': []}, environment={u'affinity:container': u'=0bf65d0d5bcebf1ad106556b46c32b2d4c0dad49f91b4a41fde9d8563423ef4c'}, volumes={u'/opt/kibana/config/kibana.yml': {}}, detach=True)
compose.cli.verbose_proxy.proxy_callable: docker create_container -> {u'Id': u'3bf3e9b086b80b47e9cc055581ae005d41133187e4c6820cf34ba9ed580d4530'}
compose.cli.verbose_proxy.proxy_callable: docker inspect_container <- (u'3bf3e9b086b80b47e9cc055581ae005d41133187e4c6820cf34ba9ed580d4530')
compose.cli.verbose_proxy.proxy_callable: docker inspect_container -> {u'AppArmorProfile': u'',
 u'Args': [u'/tmp/entrypoint.sh'],
 u'Config': {u'AttachStderr': False,
             u'AttachStdin': False,
             u'AttachStdout': False,
             u'Cmd': [u'/tmp/entrypoint.sh'],
             u'Domainname': u'',
             u'Entrypoint': [u'/docker-entrypoint.sh'],
             u'Env': [u'PATH=/opt/kibana/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
                      u'TINI_VERSION=v0.9.0',
...
compose.cli.verbose_proxy.proxy_callable: docker inspect_container <- (u'3bf3e9b086b80b47e9cc055581ae005d41133187e4c6820cf34ba9ed580d4530')
compose.cli.verbose_proxy.proxy_callable: docker inspect_container -> {u'AppArmorProfile': u'',
 u'Args': [u'/tmp/entrypoint.sh'],
 u'Config': {u'AttachStderr': False,
             u'AttachStdin': False,
             u'AttachStdout': False,
             u'Cmd': [u'/tmp/entrypoint.sh'],
             u'Domainname': u'',
             u'Entrypoint': [u'/docker-entrypoint.sh'],
             u'Env': [u'PATH=/opt/kibana/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
                      u'TINI_VERSION=v0.9.0',
...
compose.cli.verbose_proxy.proxy_callable: docker attach <- (u'3bf3e9b086b80b47e9cc055581ae005d41133187e4c6820cf34ba9ed580d4530', stderr=True, stream=True, stdout=True)
compose.cli.verbose_proxy.proxy_callable: docker attach -> <generator object _multiplexed_response_stream_helper at 0x7f92b781b4b0>
compose.cli.verbose_proxy.proxy_callable: docker disconnect_container_from_network <- (u'3bf3e9b086b80b47e9cc055581ae005d41133187e4c6820cf34ba9ed580d4530', u'dockerkibana_default')
compose.cli.verbose_proxy.proxy_callable: docker disconnect_container_from_network -> None
compose.cli.verbose_proxy.proxy_callable: docker connect_container_to_network <- (u'3bf3e9b086b80b47e9cc055581ae005d41133187e4c6820cf34ba9ed580d4530', u'dockerkibana_default', links=[], aliases=['kibana', u'3bf3e9b086'])
compose.cli.verbose_proxy.proxy_callable: docker connect_container_to_network -> None
compose.cli.verbose_proxy.proxy_callable: docker start <- (u'3bf3e9b086b80b47e9cc055581ae005d41133187e4c6820cf34ba9ed580d4530')
ERROR: compose.cli.main.main: 500 Internal Server Error: Cannot start container 3bf3e9b086b80b47e9cc055581ae005d41133187e4c6820cf34ba9ed580d4530: [9] System error: not a directory

这里我们有一个Model类的实例,我知道这只是内存中的一个对象。 function Model() { //constructor } var dummy = new Model(); // create an instance of Model dummy.self = dummy; 变量是对内存中对象的引用, dummy 属性也是对同一对象的引用。

此代码是否只创建了对象本身的两个引用?一个是 self 变量,第二个是 dummy 属性?。

如果您在Chrome开发工具中检查该代码,正如预期的那样,控制台会显示对无限的嵌套引用。

这是否会影响代码的性能?

注意:

作为参考,我有一个类,它在创建时返回一个实例,每个方法都返回一个promise。最后,当promise被解析时,实例类就是已解析的对象。

self

1 个答案:

答案 0 :(得分:1)

没有。没有性能滞后。您只是将另一个属性存储到对象。 Chrome Dev Tools无限引用,因为每次展开对象时都会对其进行探索。在这种情况下,您正在引用自身来扩展对象。