Javascript 中的几乎所有东西如何都是对象?

本文介绍了Javascript 中的几乎所有东西如何都是对象?的处理方法,对大家解决问题具有一定的参考价值

问题描述

我在许多 JavaScript 介绍中阅读了很多.我就是不明白.我一直认为对象是具有方法和属性的东西.我理解数组,因为它有键值对.字符串"或数字"或函数"怎么样?上面列出的这些东西对我来说似乎是函数.这意味着你输入一些东西,你得到一些东西.您并没有真正获得访问属性或任何东西.在数组或这个对象"列表中没有使用点符号.

I read this a lot in many JavaScript introductions. I just don't understand it. I always think of objects as something with methods and properties. Arrays I understand, since it has key value pair. How about "Strings" or "Numbers" or "functions" ? These things above listed seem to be like functions to me. This means you input something, you get something out. You don't really get the access properties or anything. There's no dot notation used in arrays or this list of "objects".

有没有人用点表示法编写了其中每个方法和属性被访问的一些示例?我怀疑对象的定义可能是有限的,因为我确实开始学习 JavaScript...

Does anyone code some examples of each of these with dot notations which its methods and properties are being accessed? I suspect that definition of object is probably limited since I did start learning about JavaScript...

推荐答案

没错:在 JavaScript 中,几乎一切都是对象.但是这些对象与我们在 Java、C++ 或其他传统语言中看到的有点不同.JS 中的对象只是一个带有键值对的哈希图.键总是字符串或符号,值可以是任何东西,包括字符串、整数、布尔值、函数、其他对象等.所以我可以像这样创建一个新对象:

That’s right: in JavaScript, almost everything is an object. But these objects are bit different from what we see in Java, C++ or other conventional languages. An object in JS is simply a hashmap with key–value pairs. A key is always a string or a symbol, and a value can be anything including strings, integers, booleans, functions, other objects etc. So I can create a new object like this:

var obj = {}; // This is not the only way to create an object in JS

并向其中添加新的键值对:

and add new key–value pairs into it:

obj['message'] = 'Hello'; // You can always attach new properties to an object externally

obj.message = 'Hello';

同样,如果我想给这个对象添加一个新函数:

Similarly, if I want to add a new function to this object:

obj['showMessage'] = function(){
  alert(this['message']);
}

obj.showMessage = function() {
  alert(this.message);
}

现在,每当我调用这个函数时,它都会显示一个带有消息的弹出窗口:

Now, whenever I call this function, it will show a pop-up with a message:

obj.showMessage();

数组只是那些能够包含值列表的对象:

Arrays are simply those objects which are capable of containing lists of values:

var arr = [32, 33, 34, 35]; // One way of creating arrays in JS

虽然你总是可以使用任何对象来存储值,但数组允许你存储它们而无需为它们中的每一个关联一个键.所以你可以使用它的索引访问一个项目:

Although you can always use any object to store values, but arrays allow you to store them without associating a key with each of them. So you can access an item using its index:

alert(arr[1]); // This would show 33

一个数组对象,就像JS中的任何其他对象一样,有它的属性,比如:

An array object, just like any other object in JS, has its properties, such as:

alert(arr.length); // This would show 4

对于深入的细节,我强烈推荐 John Resig 的 专业 JavaScript 技术.

For in-depth detail, I would highly recommend John Resig’s Pro JavaScript Techniques.

这篇关于Javascript 中的几乎所有东西如何都是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1159

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1060

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:799

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:885

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:925

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:872

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:854

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:825

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1691

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1298