`

JAVASCRIPT基础学习篇(6)--ECMAScript Basic2(EcmaScript 引用类型)

阅读更多

第二章 Reference Types引用类型:


可以使用:var o = new Object()或var o = new Object来创建一个对象
js中类与java中相似,即Object类作为所有类的基类,所有类都具有Object类所拥有的属性
All the properties and methods of the Object class are also present in the other classes, and so to understand the Object class is to understand all the others better.
The Object class has the following properties:(两个属性)
(1)constructor — A reference value (pointer) to the function that created the object. For the
Object class, this points to the native Object() function.
(2)prototype — A reference value to the object prototype for this object. Prototypes are discussed
further in Chapter 3. For the all classes, this returns an instance of Object by default.
有关这两个属性的介绍及应用分别在以下贴子中说明:
http://blog.csdn.net/luweifeng1983/archive/2009/02/20/3915189.aspx
http://blog.csdn.net/luweifeng1983/archive/2009/02/20/3915284.aspx
The Object class also has several methods:(成员方法)
(1)hasOwnProperty(property) — Determines if a given property exists for the object. The
property must be specified as a string (for example, o.hasOwnProperty(“name”)).
(2)isPrototypeOf(object) — Determines if the object is a prototype of another object.
(3)propertyIsEnumerable(property) — Determines if a given property can be enumerated by
using the for...in statement (discussed later in this chapter).
(4)toString() — Returns a primitive string representation of the object. For the Object class,
this value is undefined in ECMA-262 and, as such, differs in each implementation.
(5)valueOf() — Returns the most appropriate primitive value of this object. For many classes,
this returns the same value as toString().

一、The Boolean class
var oBooleanaobject = new Boolean(true);
Boolean类重写了toString()和valueOf(),可以使用它们返回对应的基本类型的值
需要注意的是以下表达式:
var oFalseObject = new Boolean(false);
var bResult = oFalseObject && true; //outputs true
这是因为oFalseObject为对象,对象返回值为true所以结果为true

二、The Number class
var oNumberObject = new Number(55);
var iNumber = oNumberObject.valueOf();
除了从Object类继承来的方法之外,Number类还定义了其它方法
1、toFixed()
var oNumberObject = new Number(99);
alert(oNumberObject.toFixed(2)); //outputs “99.00”
注意上面参数表示小数点位数,可以从0-20,其它值将出错
2、 以下方法转換成指数形式:
var oNumberObject = new Number(99);
alert(oNumberObject.toExponential(1)); //outputs “9.9e+1” 表示9.9 x 101

3、toPrecision返回 NumberObject 的字符串表示,包含 num 个有效数字。如果 num 足够大,能够包括 NumberObject 整数部分的所有数字,
那么返回的字符串将采用定点计数法。否则,采用指数计数法,即小数点前有一位数字,小数点后有 num-1 位数字。必要时,该数字会被舍入或用 0 补足。
var oNumberObject = new Number(99);
alert(oNumberObject.toPrecision(1)); //outputs “1e+2”

var oNumberObject = new Number(99);
alert(oNumberObject.toPrecision(2)); //outputs “99”

var oNumberObject = new Number(99);
alert(oNumberObject.toPrecision(3)); //outputs “99.0”

三、The String class
1、valueOf() and toString()
var oStringObject = new String(“hello world”);
Both valueOf() and toString() return the String primitive value for a String object:
alert(oStringObject.valueOf() == oStringObject.toString()); //outputs “true”
2、length属性
The String class has one property, length, which gives the number of characters in the string:
var oStringObject = new String(“hello world”);
alert(oStringObject.length); //outputs “11“
3、charAt()与charCodeAt()
除了从Object类继承来的方法之外,String类还定义了其它方法
var oStringObject = new String(“hello world”);
alert(oStringObject.charAt(1)); //outputs “e”

If instead of the actual character you want the character code, then calling charCodeAt() is the appropriate choice
var oStringObject = new String(“hello world”);
alert(oStringObject.charCodeAt(1)); //outputs “101”
4、concat()与+
var oStringObject = new String(“hello “);
var sResult = oStringObject.concat(“world”);
alert(sResult); //outputs “hello world”
alert(oStringObject); //outputs “hello “

使用+号运算符
var oStringObject = new String(“hello “);
var sResult = oStringObject + “world”;
alert(sResult); //outputs “hello world”
alert(oStringObject); //outputs “hello “
5、indexOf()与lastIndexOf()
var oStringObject = new String(“hello world”);
alert(oStringObject.indexOf(“o”)); //outputs “4”
alert(oStringObject.lastIndexOf(“o”)); //outputs “7”
6、The next method is localeCompare(), which helps sort string values. This method takes one argument,
the string to compare to, and it returns one of three values:
If the String object should come alphabetically before the string argument, a negative number
is returned (most often this is –1, but it is up to each implementation as to the actual value).
If the String object is equal to the string argument, 0 is returned.
If the String object should come alphabetically after the string argument, a positive number is
returned (most often this is 1, but once again, this is implementation-specific).
Example:
var oStringObject = new String(“yellow”);
alert(oStringObject.localeCompare(“brick”)); //outputs “1”
alert(oStringObject.localeCompare(“yellow”)); //outputs “0”
alert(oStringObject.localeCompare (“zoo”)); //outputs “-1”

更好的使用localeCompare()的方式如下:
var oStringObject1 = new String(“yellow”);
var oStringObject2 = new String(“brick”);
var iResult = sTestString.localeCompare(“brick”);
if(iResult < 0) {
alert(oStringObject1 + “ comes before “ + oStringObject2);
} else if (iResult > 0) {
alert(oStringObject1 + “ comes after “ + oStringObject2);
} else {
alert(“The two strings are equal”);
}
7、取子串的两个方法slice()和substring()
var oStringObject = new String(“hello world”);
alert(oStringObject.slice(3)); //outputs “lo world”
alert(oStringObject.substring(3)); //outputs “lo world”
alert(oStringObject.slice(3, 7)); //outputs “lo w”
alert(oStringObject.substring(3,7)); //outputs “lo w”
这两个方法在参数为正数的时候是一样的,不同在于当参数为负数的时候
var oStringObject= new String(“hello world”);
alert(oStringObject.slice(-3)); //outputs “rld”
alert(oStringObject.substring(-3)); //outputs “hello world”
alert(oStringObject.slice(3, -4)); //outputs “lo w”
alert(oStringObject.substring(3,-4)); //outputs “hel”
理解:slice(-3)认为是slice(7)而substring(-3)当作是substring(0)
slice(3, -4)当作是slice(3, 7),而substring(3,-4)当作是substring(3,0)
这里要注意的是substring(3,0)与substring(0,3)是一样的,因为substring方法总是把两个参数中小的那个作为起始位,大的作为终止位。
8、toLowerCase(), toLocaleLowerCase(), toUpperCase(), and toLocaleUpperCase().
var oStringObject= new String(“Hello World”);
alert(oStringObject.toLocaleUpperCase()); //outputs “HELLO WORLD”
alert(oStringObject.toUpperCase()); //outputs “HELLO WORLD”
alert(oStringObject.toLocaleLowerCase()); //outputs “hello world”
alert(oStringObject.toLowerCase()); //outputs “hello world”

Remember, all the methods and properties for the String class also apply to String primitive values because they are pseudo-objects.

分享到:
评论

相关推荐

    Learn ECMAScript, 2nd Edition-Packt Publishing(2018).epub

    This book introduces the fresh and core concepts of JavaScript in the form of ECMAScript 2017 (ES8), which includes everything you'll need to get started with JavaScript and have a basic-to-advanced ...

    JavaScript基础.pdf

    ### JavaScript基础知识点详解 #### 一、脚本语言概述 **1.1 什么是脚本语言** 脚本语言(Scripting Language)是一种轻量级的编程语言,它使用ASCII字符编写,无需像C语言或Java那样经过编译过程转换为二进制...

    1-basic-types(基础类型1).pdf

    在TypeScript中,基础类型是构建复杂应用程序的基本构建块。这些类型包括布尔值、数字、字符串、数组、元组和枚举,它们都是JavaScript的核心部分,但TypeScript提供了额外的语义和工具来增强这些类型。 首先,布尔...

    计算机软件-编程源码-Javascript_VBScript教程专栏.zip

    1. **基础语法**:变量声明(var、let、const)、数据类型(基本类型与引用类型)、运算符、流程控制(条件语句、循环)等。 2. **函数**:函数定义、函数表达式、作用域、闭包。 3. **面向对象**:对象、类、原型链...

    Apply-JavaScript-Basic-Concept

    数据类型在JavaScript中至关重要,包括基础类型(如字符串、数字、布尔值、null和undefined)和引用类型(如对象)。JavaScript是一种动态类型语言,这意味着变量的数据类型可以在运行时改变。 控制结构包括条件...

    JavaScript Functional Programming for JavaScript Developers (PDF, EPUB, MOBI)

    Chapter 14: ECMAScript-2015/2016 Solutions Today Module 3: Functional Programming in JavaScript Chapter 1: The Powers of JavaScript's Functional Side – a Demonstration Chapter 2: Fundamentals of ...

    Speaking JavaScript

    Basic JavaScript Part II: Background Chapter 2. Why JavaScript? Chapter 3. The Nature of JavaScript Chapter 4. How JavaScript Was Created Chapter 5. Standardization: ECMAScript Chapter 6. Historical...

    JavaScript-Basic_Project2_141212:这是 Udacity 的 JavaScript Basic 的最终项目

    这个名为"JavaScript-Basic_Project2_141212"的项目是Udacity的JavaScript基础课程的最终项目,旨在帮助学习者深入理解和应用JavaScript的基础知识。 在JavaScript的世界里,基础是至关重要的。这个项目可能涵盖...

    Basic-knowledge-of-java.zip_Knowledge

    以上是JavaScript基础知识的概览,通过深入学习和实践,你可以更好地掌握这门强大的脚本语言。对于压缩包中的"Basic knowledge of java.doc"文档,很可能是针对JavaScript的基础知识进行的详细讲解,包括上述知识点...

    freeCodeCamp-Basic-JavaScript

    【免费编程营-Basic-JavaScript】是freeCodeCamp学习平台的一个重要部分,专注于教授初学者JavaScript的基础知识。JavaScript是一种广泛使用的编程语言,主要用于网页和网络应用的开发,赋予了网页动态交互的能力。...

    basic-course-javascript

    【JavaScript基础课程】 JavaScript,简称JS,是一...这个"basic-course-javascript"课程将涵盖以上这些关键知识点,并通过实例和练习帮助学习者逐步掌握JavaScript编程,为他们进一步探索Web开发世界打下坚实的基础。

    basic-projects-js:JavaScript研究

    JavaScript是Web开发中不可或缺的一部分,...通过分析和学习"basic-projects-js"中的代码,不仅能巩固JavaScript基础知识,还能了解实际项目中的应用方式,这对于提升JavaScript技能和解决实际问题的能力非常有帮助。

    basic-javascript:所有Javascript编码挑战

    "basic-javascript:所有Javascript编码挑战"是一系列专为初学者或编程新手设计的学习资源,旨在帮助他们掌握JavaScript的基础知识。以下是对标题和描述中提及的各个主题的详细说明: 1- 变量声明和用法: 在...

    javascript 和 vbscript 中文参考 帮助文件

    2. **类型系统**:JavaScript 是动态类型,VBScript 是静态类型。 3. **继承机制**:JavaScript 采用原型链,VBScript 使用类继承。 4. **执行环境**:JavaScript 有 Node.js 作为服务器端环境,VBScript 多用于...

    Java-Script-Basic-Program

    8. **ES6及新特性**:ECMAScript 6(ES6,也称为ES2015)引入了许多新特性,如箭头函数、模板字符串、解构赋值、Promise、模块系统等,增强了JavaScript的可读性和功能。 9. **闭包**:闭包是JavaScript中的一个...

    Javaweb开发 JavaScript php SQLServer2005 VisualBasic 编程技术文档1

    在IT行业中,编程技术是构建复杂系统的基础,而"Javaweb开发 JavaScript php SQLServer2005 VisualBasic 编程技术文档1"这个压缩包涵盖了几个关键的技术领域,让我们逐一深入探讨。 首先,JavaWeb开发是创建动态...

    VB Script JavaScript 脚本速查 ASP学习者必备

    2. **JavaScript基础**:变量(var关键字)、数据类型(Number, String, Boolean, Object等)、流程控制(if, switch, for, while)、函数的定义和调用。 3. **ASP核心概念**:Request对象(获取用户输入)、...

    VBScript和Javascript语言参考手册

    6. **标准支持**:JavaScript遵循ECMAScript标准,有广泛的标准库和框架,如jQuery、React等;VBScript则缺乏统一标准,生态相对封闭。 在实际应用中,VBScript逐渐被JavaScript所取代,尤其是在跨平台和移动设备...

    脚本语言vbs,JavaScript等等

    JavaScript,另一方面,是一种基于ECMAScript规范的弱类型、解释型的、动态的、多范式的脚本语言。JavaScript最初由Netscape公司的Brendan Eich设计,主要用于客户端网页的交互,使得网页具有动态效果和用户交互性。...

Global site tag (gtag.js) - Google Analytics