Javascript DOM 编程艺术:object

There is one very important data type that we haven’t looked at yet: objects. An object is a
self-contained collection of data. This data comes in two forms: properties and methods

A property is a variable belonging to an object.
A method is a function that the object can invoke.
These properties and methods are all combined in one single entity, which is the object.
Properties and methods are both accessed in the same way using JavaScript’s dot syntax:


Object.property
Object.method()

对象分类

native object

  You’ve already seen objects in action. Array is an object. Whenever you initialize an array
using the new keyword, you are creating a new instance of the Array object:
var beatles = new Array();
When you want to find out how many elements are in an array, you do so by using the
length property:
beatles.length;
The Array object is an example of a native object supplied by JavaScript.

Host objects

   Native objects aren’t the only kind of pre-made objects that you can use in your scripts.

Another kind of object is supplied not by the JavaScript language itself, but by the
environment in which it’s running. In the case of the Web, that environment is the web
browser. Objects that are supplied by the web browser are called host objects.
Host objects include Form, Image, and Element. These objects can be used to get information
about forms, images, and form elements within a web page.
I’m not going to show you any examples of how to use those host objects. There is another
object that can be used to get information about any element in a web page that you
might be interested in: the document object. For the rest of this book, we are going to be
looking at lots of properties and methods belonging to the document object.

User-Define Object(用户自定义对象

 

js数据类型

 

原始类型(primitive type):存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量访问的位置。

    数字(Number)、字符串(String)、布尔(Boolen)、Null、Undefine

  引用类型(reference type):存储在堆(heap)中的对象,也就是说,存储在变量处的值是一个指针(point),指向存储对象的内存处。

    引用类型就是对象,比如 Object,Number、String,Boolen,Date、Array 等等

  呵呵,怎么原始类型中有 String,引用类型也有 String 呢?看下面一段代码就明白它们的区别了:

//这里是 String 原始类型
var str = "string";
//这里是 String 对象
var str = new String("string");

var str1="abc"; typeof str1  string

var str2="abc";

alert(str1 === str2) true;

var str3=new String("abc"); typeof str3 Object

var str3=new String("abc");

alert(str1===str3)false

当对字符串直接量调用方法时,,字符串会临时转换为字符串包装对象。 字符串被视为好像是使用 new 运算符创建的一样。如

str1.toLowercase();

对象是什么?

  上面说了,引用类型就是对象。在 JavaScript 中,对象到底是什么?这里结合本书和 W3CSchool,给出定义:

    “特性(attribute)的无序集合”

  就这么简单?对,就这么简单。你可能会问,一个对象总应该有属性和方法吧?我们可以这样看:

  “当此特性为函数时,我们视之为对象的方法(method);不是函数,则视之为对象的属性(proporty)”

  

原文地址:https://www.cnblogs.com/youxin/p/2653292.html