创建空对象:

var o = new Object();
                o.a = "A";
                o.b = "B";
                o.c = "C";
                alert(o.a + o.b + o.c);


                ABC

对象直接量:

var o1 = {a:1, b:2, c:3};
                alert(o1.a + o1.b + o1.c);


                6

嵌套的对象:

var o = new Object();
                o.a = "A";
                o.b = "B";
                o.c = "C";
                o.d = {x:1, y:2, z:3};
                alert(o.a + o.b + o.c + o.d.x + o.d.y + o.d.z);


                ABC123

未定义的对象属性:

var o = new Object();
                o.a = "A";
                o.b = "B";
                o.c = "C";
                o.d = {x:1, y:2, z:3};
                o.a = o.no_such_property;
                delete o.b;
                o.c = null;
                o.d.x = void 0;
                alert(o.a + ',' + o.b + ',' + o.c + ',' + o.d.x + ',' + o.d.y + o.d.z);


                undefined,undefined,null,undefined,23

枚举对象中的属性名:

var o = new Object();
                o.a = "A";
                o.b = "B";
                o.c = "C";
                o.d = {x:1, y:2, z:3};
                var s = "";
                for (i in o)
                {
                    s += i + ",";
                }
                alert(s);


                a,b,c,d,       

构造函数:

function init()
            {
                function Book(name, author)
                {
                    this.name = name;
                    this.author = author;
                }
                var book1 = new Book('The Republic', 'Plato');
                var book2 = new Book('A History of Western Philosophy', 'Russel');
                alert("<" + book1.name + ">(" + book1.author + ")");
                alert("<" + book2.name + ">(" + book2.author + ")");
            }


<The Republic>(Plato)

<A History of Western Philosophy>(Russel)

将方法赋给对象:(必须分别赋给创建的每一个对象)

function Book(name, author)
                {
                    this.name = name;
                    this.author = author;
                }
                function toString()
                {
                    return "<" + this.name + ">(" + this.author + ")";
                }
                var book1 = new Book('The Republic', 'Plato');
                book1.toString = toString;
                var book2 = new Book('A History of Western Philosophy', 'Russel');
                book2.toString = toString;
                alert(book1.toString());
                alert(book2.toString());


             <The Republic>(Plato)

<A History of Western Philosophy>(Russel)

将方法的赋值放在构造函数里:

function Book(name, author)
                {
                    this.name = name;
                    this.author = author;
                    this.toString = toString;
                }
                function toString()
                {
                    return "<" + this.name + ">(" + this.author + ")";
                }
                var book1 = new Book('The Republic', 'Plato');
                var book2 = new Book('A History of Western Philosophy', 'Russel');
                alert(book1.toString());
                alert(book2.toString);


<The Republic>(Plato)

function toString()
                {
                    return "<" + this.name + ">(" + this.author + ")";
                }

prototype原型:

function init()
        {
            function Book(title, author)
            {
                this.title = title;
                this.author = author;
            }
            function toString()
            {
                return this.title + "," + this.author + "," + this.subject;
            }
            Book.prototype.subject = "Philosophy";
            Book.prototype.toString = toString;
            var book = new Book("Republic", "Plato");
            document.write(book.toString() + "<br>");
            for (i = 1;i <= 5; i++)
            {
                book = new Book("T" + i, "A" + i)
                if (i % 2 == 0) Book.prototype.subject = "Maths";
                else Book.prototype.subject = "Chemistry";
                document.write(book.toString() + "<br>");
            }
        }



Republic,Plato,Philosophy
T1,A1,Chemistry
T2,A2,Maths
T3,A3,Chemistry
T4,A4,Maths
T5,A5,Chemistry

即使属性是在对象被创建以后才添加到它的原型对象中的,对象也能够继承这些属性:

function init()
        {
            function Book(title, author)
            {
                this.title = title;
                this.author = author;
            }
            function toString()
            {
                var v = "";
                for (s in this)
                {
                    v+= s + ",";
                }
                return v;
            }
            Book.prototype.toString = toString;
            var book = new Book("Republic", "Plato");
            document.write(book.toString() + "<br>");
            Book.prototype.subject = "Philosophy";
            document.write(book.toString() + "<br>");  
        }



title,author,
subject,title,author,

关于prototype,一个奇怪的问题:

function Book(title, author)
            {
                this.title = title;
                this.author = author;
            }
            function getFields(O)
            {
                var v = "";
                for (s in O)
                {
                    v+= s + "=" + O[s] + ",";
                }
                return v;
            }
            var A = new Book("Ta", "Aa");
            var B = new Book("Tb", "Ab");
            document.write("--------------------------<br>");
            document.write(getFields(A) + "<br>");
            document.write(getFields(B) + "<br>"); 
            B.pages = 508;
            B.price = "$20.50";
            document.write("--------------------------<br>");
            document.write(getFields(A) + "<br>");
            document.write(getFields(B) + "<br>");
            Book.prototype = B;
            document.write("--------------------------<br>");
            document.write(getFields(A) + "<br>");
            document.write(getFields(B) + "<br>"); 
            document.write("--------------------------<br>");
            document.write(getFields(Book.prototype) + "<br>");
            var C = new Book("Tc", "Ac");
            document.write("--------------------------<br>");
            document.write(getFields(C) + "<br>");



--------------------------
title=Ta,author=Aa,
title=Tb,author=Ab,
--------------------------
title=Ta,author=Aa,
title=Tb,author=Ab,pages=508,price=$20.50,
--------------------------
title=Ta,author=Aa,

//在“Book.prototype=B”执行以后,对象A并没有获得B中的两个新属性。奇怪。新创建的对象C却获得了。
title=Tb,author=Ab,pages=508,price=$20.50,
--------------------------
title=Tb,author=Ab,pages=508,price=$20.50,
--------------------------
price=$20.50,pages=508,title=Tc,author=Ac,

面向对象的JavaScript:

function Book(title, author)
            {
                this.title = title; //实例变量
                this.author = author;
            }
            Book.prototype.toString = function() //实例方法
            {
                var a = "Book[";
                for (s in this)
                {
                    a += s + "=" + this[s] + ";";
                }
                a += "]";
                return a;
            }
            Book.toString = function(book) //静态方法
            {
                var a = "Book[";
                for (s in book)
                {
                    a += s + "=" + book[s] + ";";
                }
                a += "]";
                return a;
            }
            var A = new Book("Ta", "Aa");
            Book.KIND = "book"; //静态变量
            document.write(A.KIND + ";" + Book.KIND + "<br>");
            document.write(A.toString() + "<br>");
            document.write(Book.toString(A) + "<br>");


undefined;book
Book[title=Ta;author=Aa;]
Book[title=Ta;author=Aa;]


undefined;book
Book[title=Ta;author=Aa;]
Book[title=Ta;author=Aa;]

[JavaScript]Object(对象)学习相关推荐

  1. JavaScript Object对象

    原文:JavaScript Object对象 Object对象 1. 介绍 Object对象,是所有JavaScript对象的超类(基类).Object.prototype(Obecjt的原型)定义了 ...

  2. JavaScript学习系列3 -- JavaScript arguments对象学习

    在实际项目开发中,目前还是很少使用到JavaScript 中的arguments对象,那么它到底是干什么用的呢 arguments是JavaScript中的一个类数组对象,它代表传给一个正在执行的函数 ...

  3. JavaScript之对象学习

    对象是一种非常重要的数据类型,他是一种自包含的数据集合,包含在对象里面的数据可以通过属性和方法两种形式来访问; 1.属性是隶属于某个特定对象的变量; 2.方法是只有某个特定对象才能调用的函数; 而对象 ...

  4. Scala学习教程笔记二之函数式编程、Object对象、伴生对象、继承、Trait、

    1:Scala之函数式编程学习笔记: 1:Scala函数式编程学习:1.1:Scala定义一个简单的类,包含field以及方法,创建类的对象,并且调用其方法:class User {private v ...

  5. JavaScript 复制对象与Object.assign方法无法实现深复制

    在JavaScript这门语言中,数据类型分为两大类:基本数据类型和复杂数据类型.基本数据类型包括Number.Boolean.String.Null.String.Symbol(ES6 新增),而复 ...

  6. JavaScript:对Object对象的一些常用操作总结

    JavaScript对Object对象的一些常用操作总结. 一.Object.assign() 1.可以用作对象的复制 var obj = { a: 1 }; var copy = Object.as ...

  7. 《JavaScript启示录》——1.21 JavaScript对象和Object()对象

    本节书摘来自异步社区<JavaScript启示录>一书中的第1章,第1.21节,作者:[美]Cody Lindley著,更多章节内容可以访问云栖社区"异步社区"公众号查 ...

  8. 详解Javascript中的Object对象

    本文地址:http://luopq.com/2016/02/28/Object-in-Javascript/,转载请注明 Object是在javascript中一个被我们经常使用的类型,而且JS中的所 ...

  9. 【JavaScript】Document对象学习

    Document 对象 当浏览器载入 HTML 文档, 它就会成为 Document 对象. Document 对象是 HTML 文档的根节点. Document 对象使我们可以从脚本中对 HTML ...

  10. 【JavaScript】Window对象学习

    Javascript组成 JavaScript的实现包括以下3个部分: 1.核心(ECMAScript):描述了JS的语法和基本对象. 2.文档对象模型 (DOM):处理网页内容的方法和接口. 3.浏 ...

最新文章

  1. 用Python实现Gauss-Jordan求逆矩阵
  2. qt 中的 quit() close()与 exit()
  3. JQuery框架2.位置属性|筛选方法|事件
  4. python 工资管理软件_智慧职教云课堂2020Python程序设计(深圳信息职业技术学院)题目答案...
  5. 第三十五期:当我们在讨论CQRS时,我们在讨论些神马?
  6. 【C++深度剖析教程37】类模板的概念和意义
  7. 国土空间规划基数转换初探
  8. proc源码解析(一)--proc文件系统的内容
  9. C#中只使用Invokerequired来判断是不是UI线程可靠吗?
  10. RFID--Radio frequency Identification
  11. 杰控连接mysql_杰控组态手册22.数据库连接.pdf
  12. 威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
  13. java 获取html title_java htmlparser 获取网页title
  14. 洛克菲勒写给儿子的38封信(上)、起点不决定终点、别让精神破产、我奋斗,我成功、我不依赖天赐的运气,但我靠策划运气平步青云、后退就是投降
  15. Linux查看及测试网络
  16. 洛谷P3835 【模板】可持久化平衡树(FHQ Treap)
  17. 淘宝API接口调用:item_search_img - 按图搜索淘宝商品(拍立淘)
  18. 手把手教你建立私人数据检索库(二)
  19. c++成员变量初始化
  20. ECM是什么-企业内容管理

热门文章

  1. python电影系统管理-Python 爬取电影网站的信息【如有重复请管理删帖】
  2. uts大学计算机排名,uts是什么大学
  3. 方法教程:一分钟把网易云音乐上的MV/mv视频下载到本地电脑
  4. CF869E The Untended Antiquity(二维数状数组+差分+hash)
  5. java 获取 yyyymmdd_从JS日期对象获取YYYYMMDD格式的字符串?
  6. 虹科分享 | 基于流的流量分类的工作原理 | 网络流量监控
  7. DS栈—波兰式,逆波兰式
  8. Win10《芒果TV》更新v3.8.30流星版:优化稳定性、升级无边框播放体验
  9. [开源][130522]DIY简易红外遥控信号发射器
  10. 带动画的好看的按钮集合HTML + CSS各种好看按钮组件