在javascript语言里面,一切皆是对象,对象是它的灵魂,值得我们学习和领悟对象的概念和使用,下面我会引用实例来进行说明。

1)创建对象

方法一:js的对象方法构造

var cody = new Object();   //produces an Object() object

cody.living = true;

cody.age = 33;

cody.gender = 'male';

cody.getGender = function(){return cody.gender;};

console.log(cody.getGender()); // 方法,logs 'male'

console.log(cody.age);  //属性

console.log(cody);    //输出整个对象

方法二,自己写构造函数完成对象创建

var Person = function(living, age, gender) {

this.living = living;

this.age = age;

this.gender = gender;

this.getGender = function() {return this.gender;};
};

// instantiate a Person object and store it in the cody variable

var cody = new Person(true, 33, 'male');

console.log(cody);

方法三,数组创建

// instantiate an Array object named myArray

var myArray = new Array(); // myArray is an instance of Array

// myArray is an object and an instance of Array() constructor

console.log(typeof myArray); // logs object! What? Yes, arrays are type of object

console.log(myArray); // logs [ ]

console.log(myArray.constructor); // logs Array()

其它方法,javascript 语言就是一门非常灵活的web开发语言,它没有严格的类型。它的灵活性很大就体现在它的对象设计思想上面,下面是九种预置的对象构造函数。我们也可以使用它们来创建我们想要的对象。

✴ Number()
✴ String()
✴ Boolean()
✴ Object()
✴ Array()
✴ Function()
✴ Date()
✴ RegExp()
✴ Error()

总结一下,javascript创建对象的方法就分两类,一种是我们自己构造的对象(如方法二);另一种就是利用javascript内置的构造函数进行构造的。

javascript系列学习----Creating objects的更多相关文章

  1. Java中如何创建一个新的对象的/Creating Objects/

    The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't ...

  2. JavaScript - Standard built-in objects

    标准对象分类 Value Properties 以下全局属性返回一个简单的值:它们没有属性或者方法: Infinity NaN undefined null literal Function Prop ...

  3. [Javascript] Simplify Creating Immutable Data Trees With Immer

    Immer is a tiny library that makes it possible to work with immutable data in JavaScript in a much m ...

  4. [Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别

    条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...

  5. Creating objects on stack or heap

    class Player {  private: int health;  int strength;  int agility; public: void move(); void attackEn ...

  6. javascript系列学习----对象相关概念理解

    1.构造函数(相对于面向对象编程语言里面的类) 2.对象实例(它是由构造函数构造出来的对象,使用到关键字 new) 3.this关键字(往往是指我们的对象本身) 下面我们来看一个实例: var Per ...

  7. JavaScript Objects in Detail

    JavaScript’s core—most often used and most fundamental—data type is the Object data type. JavaScript ...

  8. JavaScript Garden

    Objects Object Usage and Properties Everything in JavaScript acts like an object, with the only two ...

  9. JavaScript- The Good Parts Chapter 3 Objects

    Upon a homely object Love can wink.—William Shakespeare, The Two Gentlemen of Verona The simple type ...

随机推荐

  1. yii控制布局方式

    1:在控制器内成员变量设置 public $layout = false; //不使用布局 public $layout = “main”; //设置使用的布局文件 2:在控制器成员方法内设置 $th ...

  2. day6-面向对象基础篇

    一.面向对象引子及概念 结合编程的一些理论知识和实践,可以总结出目前存在以下编程模式: 1. 面向过程 按照业务逻辑和实现过程步骤来逐步垒代码,代码编写的逻辑即对应于实际实现的步骤过程,核心是过程两个 ...

  3. Spring报错:java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

    感谢:http://blog.chinaunix.net/uid-20681545-id-184633.html提供的解决方案,非常棒 ! 问题说明: 新建一个Spring项目,新建一个Bean类:H ...

  4. css引用第三方字体库

    对应的CSS文件中如下方式进行字体库的引用: @font-face { font-family: '造字工房情书'; src: url('../fonts/MFQingShu_Noncommercia ...

  5. 课堂/会议同屏教学解决方案之RTSP/RTP over UDP组播解决方案

    问题 在之前的博客<EasyIPCamera实现Windows PC桌面.安卓Android桌面同屏直播,助力无纸化会议系统>我们描述了一套基于EasyIPCamera的同屏功能,但是这个 ...

  6. Chrome调试javacript禁止缓存

    /********************************************************************* * Chrome调试javacript禁止缓存 * 说明: ...

  7. learn Linux sed command

    learn Linux sed command 一.参考文档: . sed命令详解 http://qifuguang.me/2015/09/21/sed%E5%91%BD%E4%BB%A4%E8%AF ...

  8. UNIX 基础知识

    登陆       1.登录名            系统在其 口令文件(通常是/etc/passwd文件) 中查看用户名,口令文件中包含了有关用户的信息.       2.shell          ...

  9. 模块(Modules)

    一.引入模块 模块:当编写更大的应用程序时,所有的代码肯定会分成多个文件,这样便于维护,另外已经编写好的函数和对象在被多个程序中使用时,不用把函数和对象拷贝到每个程序中. 模块支持以上功能,在Pyth ...

  10. Jmeter用表格查看结果

    Sample#:编号类似id Start Time:开始时间 Thread Name:线程名称 Label:请求名称 Sample Time:取样时间ms Status:状态 Bytes:接受字节数 ...