Advanced Object Creation

A constructor is a function you call to instantiate and initialize a particular type of object. You invoke a constructor with the new keyword. Here are a few examples of using constructors.
   一个构造器是一个函数,你可以调用它实例化和初始化一个特殊的对象类型。你可以使用new关键字调用一个构造器。这里有一些使用构造器的例子:

var myObject = new Object();             // Creates a generic object with no properties.

var myBirthday = new Date(1961, 5, 10);  // Creates a Date object.

var myCar = new Car();                   // Creates a user defined object, and initializes its properties.

The constructor is passed a reference to a newly created empty object as the value of the special this keyword. It is then responsible for performing appropriate initialization for the new object (creating properties and giving them initial values). When completed, the constructor returns a reference to the object it constructed.

构造器将被传递一个引用到一个新创建的空对象,作为特色的this关键字的值。这时它会负责适当的初始化这个新对象(创建属性和赋予初始值)。当完成的时候,构造器返回一个它构造的对象的引用。

Writing Constructors 写构造器

You can create objects and initialize them using the new operator in conjunction with predefined constructor functions such as Object(), Date(), and Function(). A powerful feature of object-oriented programming is the ability to define custom constructor functions to create custom objects for use in your scripts. You create custom constructors so you can create objects with properties already defined. Here is an example of a custom constructor (note the use of the this keyword).

你可以使用new创建对象并初始化它们,需要使用预先定义的函数,例如objcet(),Data()或者Function().一个强有力的对象导向程序的特质是在脚本它可以自定义构造器去创造自定义对象来使用。你可以创造自定义的构造器,所以你可以使用已经定义的属性创建对象。这里有一个自定义构造器的例子(注意this关键字的使用)。

function Circle (xPoint, yPoint, radius) {

this.x = xPoint;  // The x component of the center of the circle.

this.y = yPoint;  // The y component of the center of the circle.

this.r = radius;  // The radius of the circle.

}

When you invoke the Circle constructor, you supply values for the circle's center point and the radius (these elements are all that is needed to completely define a unique circle object). You end up with a Circle object that contains three properties. Here is how you would instantiate a Circle object.

当你调用Circle构造器的时候,你为其提供了中点和半径的值(这些元素是完全定义一个唯一的Circle对象的全部)。这里以创建一个包含3个属性的Circle对象告终。下面告诉你如何实例化一个circle对象:

var aCircle = new Circle(5, 11, 99);

Using Prototypes to Create Objects 使用prototype来创建对象

When you write a constructor, you can use properties of the prototype object (which is itself a property of every constructor) to create inherited properties, and shared methods. Prototype properties and methods are copied by reference into each object of a class, so they all have the same values. You can change the value of a prototype property in one object, and the new value overrides the default, but only in that one instance. Other objects that are members of the class are not affected by the change. Here is an example that makes use of the custom constructor, Circle (note the use of the this keyword).

当你写构造器的时候,你可以使用对象的prototype属性(它是每个构造器都有点属性)来创建内在的属性并且公开方法。Prototype属性和方法被复制到一个类的每个对象。因此他们有同样的值。你可以在一个对象中改变prototype属性的值,新值将覆盖默认的值,但是仅仅只改变这个实例。其他对象所有方法不会受到这个变化的影响。这里有一个关于自定义构造器的例子,circle(注意this关键字的使用)

Circle.prototype.pi = Math.PI;

function ACirclesArea () {

return this.pi * this.r * this.r; // The formula for the area of a circle is Ïr2.

}

Circle.prototype.area = ACirclesArea; // The function that calculates the area of a circle is now a method of the Circle Prototype object.

var a = ACircle.area();     // This is how you would invoke the area function on a Circle object.

Using this principle, you can define additional properties for predefined constructor functions (which all have prototype objects). For example, if you want to be able to remove leading and trailing spaces from strings (similar to VBScript's Trim function), you can create your own method on the String prototype object, and all strings in your script will automatically inherit the method.

使用这样的原理,你可以定义附加的属性对象预先定义的狗在其函数(所有包含prototype对象).例如,如果你想移除字符串前置和后缀的空格(类似VBScript 的TRIM函数),你可以创建你自己的方法在string的prototype对象,那么在你的脚本里所有的string将自动实现内在的方法

// Add a function called trim as a method of the prototype

// object of the String constructor.

String.prototype.trim = function()

{

// Use a regular expression to replace leading and trailing

// spaces with the empty string

return this.replace(/(^\s*)|(\s*$)/g, "");

}

// A string with spaces in it

var s = "    leading and trailing spaces    ";

// Displays "    leading and trailing spaces     (35)"

window.alert(s + " (" + s.length + ")");

// Remove the leading and trailing spaces

s = s.trim();

// Displays "leading and trailing spaces (27)"

window.alert(s + " (" + s.length + ")");

转载于:https://www.cnblogs.com/yfx1982/archive/2008/05/05/1183455.html

Advanced Object Creation(JS,翻译MSDN文章)相关推荐

  1. 【转】第7篇:Xilium CefGlue 关于 CLR Object 与 JS 交互类库封装报告:全自动注册与反射方法分析...

    作者: 牛A与牛C之间 时间: 2013-12-12 分类: 技术文章 | 2条评论 | 编辑文章 主页 » 技术文章 » 第7篇:Xilium CefGlue 关于 CLR Object 与 JS ...

  2. Error creating bean with name ‘processEngine‘: FactoryBean threw exception on object creation; neste

    Exception sending context initialized event to listener instance of class org.springframework.web.co ...

  3. 知乎登录js逆向及文章爬取js逆向

    知乎登录js逆向及文章爬取js逆向 **在此声明:**本文章仅仅用于学习交流,不得用于商业活动. 登录支持账号密码登录及知乎移动端软件扫码登录. 文章爬取是把原文章的原样近似爬取,包括图片,链接,及评 ...

  4. 调用百度API写了一个js翻译小工具

    目前还未完成的功能有:textarea高度自适应,移动端与pc端都写了. 效果如图: html: <!DOCTYPE html> <html lang="en"& ...

  5. html静态页面引用其他页面,Shtml完美解决静态页面内部调用其他页面(非Iframe、Object、Js方法)...

    我想这个是所有前端工程师都会碰到的问题,在你做了很多页面,需要调用同一个头部或者底部的时候,需要嵌套一下,这个时候怎么办 Iframe.Object.Js调用的方法就不讨论了,网上搜索一大堆,不过兼容 ...

  6. md文档html显示+toc,使用md-toc.js来生成文章目录

    Home Subscribe 使用md-toc.js来生成文章目录 07 December 2013 on 动态文档目录 使用markdown语法来博,绝逼一大利器,再配合各种静态博客技术,更是如虎添 ...

  7. FCOS:Fully Convolutional One-Stage Object Detection 论文翻译(非解读)

    全卷积单级目标检测器 摘要: 1.介绍 2.相关工作 3.方法 3.1. 全卷积单级目标探测器 3.2. FCOS的FPN多级预测 3.3. Center-ness for FCOS 4. 实验 4. ...

  8. 火车头采集器文章翻译插件(文章标题内容中英双语对照|自动插入相关图片)

    火车头采集器文章翻译插件(文章标题内容中英双语对照|自动插入相关图片) 为了保护接口压力防止被封IP: 请把采集的间隔时间调整为10000~100000 火车头采集器文章翻译插件(文章标题内容中英双语 ...

  9. A Survey of Deep Learning-based Object Detection论文翻译 + 阅读笔记

    A Survey of Deep Learning-based Object Detection论文翻译 + 阅读笔记 //2022.1.7 日下午16:00开始阅读 双阶段检测器示意图 单阶段检测器 ...

  10. Object Detection经典代码与文章

    转载自Object Detection Papers Deep Neural Networks for Object Detection paper: http://papers.nips.cc/pa ...

最新文章

  1. c语言产生一m序列,其特征多相式:1+x^3+x^5,M序列伪随机码在测距回答概率控制中的 - FPGA/ASIC技术 - 电子发烧友网...
  2. The Tail at Scale
  3. 【C++ grammar】C++简化内存模型
  4. CSDN的常用文本设置(字体大小红色)
  5. linux AB测试
  6. 蚂蚁集团:中签号码共有701696个
  7. Oracle 执行长SQL
  8. 2016 1月1日-1月1日python 学习总结
  9. 2022 Google IO大会新技术
  10. CC2530+PA(CC2590)开启功率放大模块功能说明
  11. 艺术字生成工具 | Mixlab创意编程
  12. nginx支持text html,BT面板重启Nginx提示“nginx: [warn] duplicate MIME type “text/html””解决办法...
  13. 硬盘坏了数据可以恢复吗?
  14. nacos 服务注册报错server is DOWNnow, detailed error message: Optional[Distro protocol is not initialized]
  15. 面试复习题--锁的细枝末节
  16. C++ AMP实战:绘制曼德勃罗特集图像
  17. 江苏省的计算机二级考试c语言
  18. DSP TMS320F280049之模拟比较器CMPSS(寄存器版)
  19. 拓嘉辰丰电商:多多国际入驻,需要什么资质条件
  20. OV7670无FIFO读寄存器成功

热门文章

  1. 快速从mysqldump文件中恢复一个表
  2. docker理念:不可变基础设施
  3. MapReduce运行流程分析
  4. [C++] socket - 4 [线程同步 简单例子]
  5. day20 文件上传
  6. 理解 LSTM 及其图示
  7. swoole怎么做mysql连接池_Swoole4创建Mysql连接池
  8. webpack路径问题总结
  9. 创建一个Django项目
  10. 使用kubeadm安装kubenetes