几乎Javascript中的所有内容都是一个对象? [英] How is almost everything in Javascript an object?

查看:107
本文介绍了几乎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, 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 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 it's index:

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

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

An array object, just like any other object in JS, has it's properties, such as:

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

深入细节,我强烈推荐John Resig的 Pro Javascript技术

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

这篇关于几乎Javascript中的所有内容都是一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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