在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. 极小极大搜索 的个人理解(alpha-beta剪枝)

    极小极大搜索的算法过程: 参考文档:http://www.xqbase.com/computer/search_minimax.htm (经典) 主要思想比较简单,但说清楚也不大容易.其核心思想是通过 ...

  2. 控制语句2:循环:for 与 while

    循环是所有编程语言的必备利器,用于重复的动作等等. python中的循环有何特殊性:for 与 while 都有自己的else分支. 要学会刹车: 1.条件的控制次数 2.break 与 contin ...

  3. *SCM-MANAGERtomcat寄宿使用

    采用的部署方式 TomCat 一个端口下部署多个 Application供不同部门使用 初始部署详参见 SCM-MANAGER 博文 日常使用添加部门操作步骤 从“D:\tomcat\webapps” ...

  4. Django WSGI,MVC,MTV,中间件部分,Form初识

    一.什么是WSGI? WEB框架的本质是一个socket服务端接收用户请求,加工数据返回给客户端(Django),但是Django没有自带socket需要使用 别人的 socket配合Django才能 ...

  5. CUDA库函数module management

    http://horacio9573.no-ip.org/cuda/group__CUDA__MODULE_ga52be009b0d4045811b30c965e1cb2cf.html

  6. 《机器学习》第三章——LDA

    import numpy as np x=np.zeros((2,17)) y=np.zeros((1,17)) def dataload(filename): f=open(filename) ar ...

  7. OPEN(SAP) UI5 学习入门系列之一:扫盲与热身(上)

    什么是SAP Fiori? 了解SAP UI5必须要从SAP Fiori开始,两者概念经常被混淆,而两者也确实有着非常紧密的关系. 用过SAP的同学们都对SAP的传统的界面(SAP GUI)表示“呵呵 ...

  8. JavaScript class 使用

    /********************************************************************* * JavaScript class 使用 * 说明: * ...

  9. 真机调试watch的一系列bug

    真机调试watch的一系列bug 系列一 WatchKit 2.0 app's bundle ID com.jiaoshi.memoKB is not prefixed by the parent a ...

  10. VS 工具箱 Dev控件显示

    工具箱修复Dev控件显示 用VS2010新打开一个项目,居然发现工具箱里边没了Dev控件. 命令提示符定位到Dev控件的安装目录下的\Components\Tools文件夹下,执行如下命令 添加:To ...