Javascript 中的几乎所有东西如何都是对象? [英] How is almost everything in Javascript an object?

查看:21
本文介绍了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 中的几乎所有东西如何都是对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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