search
HomeWeb Front-endJS TutorialPrototype ObjectRange object learning_prototype

Ranges represent an interval of values. The value type just needs to be “compatible,” that is, to implement a succ method letting us step from one value to the next (its successor).

Prototype provides such a method for Number and String, but you are of course welcome to implement useful semantics in your own objects, in order to enable ranges based on them.

ObjectRange objects basically implement continuous numbers or strings, where Contains only one method, include, to determine whether a number or string is in the ObjectRange. And the ObjectRange object is also mixed with Enumerable methods, so you can directly call the methods in the Enumerable object on the ObjectRange object.

Copy code The code is as follows:

//Convenient method to create ObjectRange
function $ R(start, end, exclusive) {
return new ObjectRange(start, end, exclusive);
}

//Create an ObjectRange object and inherit from Enumerable
var ObjectRange = Class. create(Enumerable, (function() {
//Initialization method, when exclusive is true, the end value is not included. The default is undefined, which is equivalent to false
function initialize(start, end, exclusive) {
this.start = start;
this.end = end;
this.exclusive = exclusive;
}

//Override the _each method in Enumerable when traversing the ObjectRange object You need to use this method
function _each(iterator) {
var value = this.start;
while (this.include(value)) {
iterator(value);
value = value.succ();
}
}

//Determine whether a value or string is included in the ObjectRange object
function include(value) {
if (value return false;
if (this.exclusive)
return value return value }

return {
initialize: initialize,
_each: _each,
include: include
};
})());

Look Let’s take an example and explain some details in detail:
Copy the code The code is as follows:

$A ($R('a', 'e'))
// -> ['a', 'b', 'c', 'd', 'e'], no surprise there

//Do not try to output the results returned below, otherwise the browser will die directly
$A($R('ax', 'ba'))
// -> Ouch! Humongous array, starting as ['ax', 'ay', 'az', 'a{', 'a|', 'a}', 'a~'...]

Let’s talk about $A($R('a', 'e')) and how to return a value. Let’s first look at the $A method. The $A method has been explained in detail in the previous article [Prototype Learning - Tool Function Learning ($A Method)]. If you don’t know, please refer to it yourself. There is this sentence in the $A method: if ('toArray' in Object(iterable)) return iterable.toArray(); We know that ObjectRange is mixed with the methods in Enumerable, which means that the toArray method is indirectly implemented, so see Let’s take a look at the toArray method in Enumerable:
Copy the code The code is as follows:

function toArray() {
return this.map();
}

//======> this.map()

//We noticed that when returning, the map method is mapped to the collect method
return {
//...
collect: collect,
map: collect,
// ...
}

//======> collect()

//In this case, this method is actually equivalent to returning an array, because passing All incoming parameters are undefined. There is a this.each method here, continue to look at
function collect(iterator, context) {
iterator = iterator || Prototype.K;
var results = [];
this.each( function(value, index) {
results.push(iterator.call(context, value, index));
});
return results;
}

// ======> this.each()

//Finally I saw this._each. Now I understand why the _each method is overridden in ObjectRange. Use this method when traversing
function each(iterator, context) {
var index = 0;
try {
this._each(function(value) {
iterator. call(context, value, index );
});
} catch (e) {
if (e != $break) throw e;
}
return this;
}

//======> this._each()

//Explain this._each() in detail
//The key is succ() method, because this method is used in _each to generate the next value.
//Where is this succ()? This method is defined in Number.prototype and String.prototype
function _each(iterator) {
var value = this.start;
while (this.include(value)) {
iterator( value);
value = value.succ();
}
}

//I won’t go into the following two methods.

//======> String.prototype.succ()

function succ() {
return this.slice(0, this.length - 1)
String.fromCharCode(this.charCodeAt(this.length - 1) 1);
}

//======> Number.prototype.succ()

function succ() {
return this 1;
}

//In summary, if you want to define other types of ObjectRange objects, such as Date type, then you can You need to implement the succ() method yourself to generate continuous objects

The above process will be clear, but some functions are not explained in detail. When these objects are mentioned, the functions inside will be explained in detail. . Let’s look at a few examples of include:
Copy the code The code is as follows:

$R( 1, 10).include(5)
// -> true
$R('a', 'h').include('x')
// -> false
$R(1, 10).include(10)
// -> true
$R(1, 10, true).include(10)
// -> false
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
使用PHP的json_encode()函数将数组或对象转换为JSON字符串使用PHP的json_encode()函数将数组或对象转换为JSON字符串Nov 03, 2023 pm 03:30 PM

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,已经成为Web应用程序之间数据交换的常用格式。PHP的json_encode()函数可以将数组或对象转换为JSON字符串。本文将介绍如何使用PHP的json_encode()函数,包括语法、参数、返回值以及具体的示例。语法json_encode()函数的语法如下:st

源码探秘:Python 中对象是如何被调用的?源码探秘:Python 中对象是如何被调用的?May 11, 2023 am 11:46 AM

楔子我们知道对象被创建,主要有两种方式,一种是通过Python/CAPI,另一种是通过调用类型对象。对于内置类型的实例对象而言,这两种方式都是支持的,比如列表,我们即可以通过[]创建,也可以通过list(),前者是Python/CAPI,后者是调用类型对象。但对于自定义类的实例对象而言,我们只能通过调用类型对象的方式来创建。而一个对象如果可以被调用,那么这个对象就是callable,否则就不是callable。而决定一个对象是不是callable,就取决于其对应的类型对象中是否定义了某个方法。如

使用Python的__contains__()函数定义对象的包含操作使用Python的__contains__()函数定义对象的包含操作Aug 22, 2023 pm 04:23 PM

使用Python的__contains__()函数定义对象的包含操作Python是一种简洁而强大的编程语言,提供了许多强大的功能来处理各种类型的数据。其中之一是通过定义__contains__()函数来实现对象的包含操作。本文将介绍如何使用__contains__()函数来定义对象的包含操作,并且给出一些示例代码。__contains__()函数是Pytho

使用Python的__le__()函数定义两个对象的小于等于比较使用Python的__le__()函数定义两个对象的小于等于比较Aug 21, 2023 pm 09:29 PM

标题:使用Python的__le__()函数定义两个对象的小于等于比较在Python中,我们可以通过使用特殊方法来定义对象之间的比较操作。其中之一就是__le__()函数,它用于定义小于等于比较。__le__()函数是Python中的一个魔法方法,并且是一种用于实现“小于等于”操作的特殊函数。当我们使用小于等于运算符(<=)比较两个对象时,Python

详解Javascript对象的5种循环遍历方法详解Javascript对象的5种循环遍历方法Aug 04, 2022 pm 05:28 PM

Javascript对象如何循环遍历?下面本篇文章给大家详细介绍5种JS对象遍历方法,并浅显对比一下这5种方法,希望对大家有所帮助!

Python中如何使用getattr()函数获取对象的属性值Python中如何使用getattr()函数获取对象的属性值Aug 22, 2023 pm 03:00 PM

Python中如何使用getattr()函数获取对象的属性值在Python编程中,我们经常会遇到需要获取对象属性值的情况。Python提供了一个内置函数getattr()来帮助我们实现这个目标。getattr()函数允许我们通过传递对象和属性名称作为参数来获取该对象的属性值。本文将详细介绍getattr()函数的用法,并提供实际的代码示例,以便更好地理解。g

使用Python的isinstance()函数判断对象是否属于某个类使用Python的isinstance()函数判断对象是否属于某个类Aug 22, 2023 am 11:52 AM

使用Python的isinstance()函数判断对象是否属于某个类在Python中,我们经常需要判断一个对象是否属于某个特定的类。为了方便地进行类别判断,Python提供了一个内置函数isinstance()。本文将介绍isinstance()函数的用法,并提供代码示例。isinstance()函数可以判断一个对象是否属于指定的类或类的派生类。它的语法如下

PHP代码封装技巧:如何使用类和对象封装可重复使用的代码块PHP代码封装技巧:如何使用类和对象封装可重复使用的代码块Jul 29, 2023 pm 11:19 PM

PHP代码封装技巧:如何使用类和对象封装可重复使用的代码块摘要:在开发中,经常遇到需要重复使用的代码块。为了提高代码的可维护性和可重用性,我们可以使用类和对象的封装技巧来对这些代码块进行封装。本文将介绍如何使用类和对象封装可重复使用的代码块,并提供几个具体的代码示例。使用类和对象的封装优势使用类和对象的封装有以下几个优势:1.1提高代码的可维护性通过将重复

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.