/*
Arrays and Objects [The Definitive Guide to JavaScript, Fifth Edition]
*/
/*
Object: is an unordered set of attributes, each attribute Each has its own name and value*/
/* Simple method to create an object, object direct quantity*/
var obj = {};
var obj = {name: 'maxthon'};
var obj = {name: {}, text: []};
/* New operator can be used*/
var a = new Array();
var d = new Date();
var r = new RegExp('javascript', 'i');
var o = new Object(); // var o = {};
/* Note: new The operator is followed by the constructor, so
typeof Array; // 'function'
typeof Object; // 'function'
Object is an instance of Function.
Function is a special object, also of Object Example.
*/
/* Object attributes*/
// Use. Conform to access the value of the attribute.
// Note: [] can be used at the same time, and attributes are used inside Name (you can use variables, this is particularly useful).
var t = {};
t.text = 'hello';
t.o = {};
t.o.name = 'rd';
t.n = [];
var t = {
"text": "hello"
};
console.log(t.text); // 'hello' ;
// Supplement: The keyword var is usually used to declare variables, but when declaring object properties, var cannot be used to declare
/* Object enumeration*/
var F = function () {};
F.prototype.name = 'RD';
var obj = new F;
for (var key in obj) {
console.log(key) ; // name;
}
// Only enumerate the object itself, do not look up along the prototype chain
for (var key in obj) {
if (obj.hasOwnProperty(key )) {
console.log(key); //
}
}
/* Note: for in cannot enumerate predefined properties; toString. */
/* Check attribute existence*/
window.a = 'rd';
console.log(a in window); // true;
var F = function () {};
F.prototype.name = 'RD';
var obj = new F;
console.log('name' in obj); // true;
var toString = Object.prototype.toString;
// If the object obj contains the method getName, execute it;
if (obj.getName && toString.call(obj.getName ) === '[object Function]') ) {
obj.getName();
}
// Supplement:
console.log(null == undefined); / / true;
console.log(null !== undefined); // true;
/* Delete attribute*/
delete obj.name;
// Supplement : Using the delete operator, variables declared using var cannot be deleted;
/* Object as an associative array*/
// Get object attributes:
obj.name ;
obj['name']; // Here name is a string.
// When expressed using [], the attribute name is represented by a string. Then it can be
// Add operations during operation
// Note: This property is particularly useful when it is passed as a variable.
// Also known as associative array
/* Mapping: JavaScript object handle String (attribute name) is mapped to a value. */
for (var key in obj) {
console.log(key); // key attribute name, exists here as a value.
}
/*
Common Object properties and methods
All objects in JavaScript inherit from the Object class;
1, constructor attribute.
points to its constructor.
*/
var F = function () {};
var f = new F;
console.log(f.constructor == F); / / true
// The prototype of the constructor has the attribute constructor pointing to itself;
F.prototype.constructor == F;
// Supplement:
var F = function ( ) {};
var G = function () {};
G.prototype = new F;
var g = new G;
console.log(g.constructor == F); // true;
console.log(g.constructor == G); // false;
// You can use g instanceof F;
/*
2, toString() method
*/
{'name': 'maxthon'}.toString(); // '[object Object]'
/* Arrays use toString method, Combine the elements into a string, and other objects will be converted into [object Object];
The function uses the original toString method, and the function source code will be obtained*/
['a', 'b', 1, false, [' e','f'], {}].toString();
// "a,b,1,false,e,f,[object Object]"
function t() {
console.log('test');
}
t.toString();
// Source code
/*
3, toLocalString();
Returns a localized string of the object
4, valueOf();
will be used when converting to a basic type. valueOf/toString.
5, hasOwnProperty();
6, propertyIsEnumberable();
Whether it can be enumerated;
7, isPrototyeOf();
a.isPrototyeOf(b);
If a is the prototype of b, return true;
* /
var o = {}; // new Object;
Object.prototype.isPrototyeOf(o); // true;
Object.isPrototyeOf(o); // false;
o. isPrototyeOf(Object.prototype); // false;
Function.prototype.isPrototyeOf(Object); // true;
/* [Closures exist when function instances exist. If garbage is not recycled, assignment references exist. ] */
/*
Array: an ordered collection of values;
Each value, also called an element, corresponds to a subscript;
The subscript starts from 0;
The value in the array can be of any type. Array, object, null, undefined.
*/
// Create.
var arr = [];
var arr = new Array();
var t = '';
var arr = [1,2,3, null, undefined, [], {}, t];
/* Three cases of using the new operator to create an array: */
var arr = new Array(); // [], the same as a direct quantity
var arr = new Array(5); // Length is 5; [] direct quantity cannot be achieved.
console.log(arr); // []; JavaScript engine will ignore undefined;
var arr = new Array('5'); // The value is ['5'];
var arr = new Array('test'); // The value is ['test'];
/* Related examples*/
var s = [1, 2, 3];
s[5] = 'a';
console.log(s);
[ 1, 2, 3, undefined, undefined, 'a']
/* Reading and writing of arrays*/
value = array[0];
a[1] = 3.14;
i = 2;
a[i] = 3;
a[a[i]] = a[0];
// array -> Object-> Attribute
array.test = 'rd';
// The array subscript is greater than or equal to 0, and less than an integer of 2 raised to the power of 32 minus 1.
/ / For other values, JavaScript will be converted into a string, used as the name of the object attribute, no longer a subscript.
var array = [];
array[9] = 10; / / The length of the array will become 10;
// Note: The JavaScript interpreter only allocates memory to the element with array index 9, and no other indexes.
var array = [];
array.length = 10; // Add the length of array;
array[array.length] = 4;
/* Delete array elements*/
// delete operator An array element is set to an undefined value, but the element itself still exists.
// To actually delete, you can use: Array.shift(); [Delete the first one] Array.pop(); [Delete the last one] Array .splice(); [Delete a continuous range from an array] or modify the Array.length length;
/* Related examples*/
var a = [1, 2, 3];
delete a[1];
console.log(a); // [1, undefined, 3];
/* Supplement: The Definitive Guide to JavaScript, fifth edition, page 59
by var The declared variables are permanent, that is to say, using the delete operator to delete these variables will cause an error.
But: In the developer tools, they can be deleted. And in the web page, as mentioned in the book Write.
*/
/* Array length*/
[].length;
/* Traverse the array*/
var array = [1, 2, 3, 4, 5];
for (var i = 0, l = array.length; i console.log(array[i]);
}
array.forEach(function (item, index, arr) {
console.log(item);
});
/* intercept or grow Array: Correct length, as mentioned before*/
/* Multidimensional array*/
[[1], [2]]
/* Array method*/
// join
var array = [1, 2, 3, 4, 5];
var str = array.join(); // 1,2,3,4,5
var str = array.join('-'); // 1-2-3-4-5
// Note: This method is opposite to the String.split() method;
// reverse() ;
var array = [1, 2, 3, 4, 5];
array.reverse(); // [5, 4, 3, 2, 1]
// Note: Modified original Array;
// sort();
var array = [1, 3, 2, 4, 5, 3];
array.sort();// [1, 2, 3, 3, 4, 5];
/* Note: There are undefined elements in the array, put these elements at the end*/
/* You can also customize the sorting, sort(func);
func receives two parameters. If the first parameter should be before the second parameter, then the comparison function will return a number less than 0. On the contrary, it will return a number greater than 0. If equal, return 0;
* /
array.sort(function (a, b) {
return b - a;
});
// Example: Sort from odd to even, and from small to large
[1, 2, 3, 4, 5, 6, 7, 2, 4, 5, 1].sort(function (a, b) {
if (a % 2 && b % 2) {
return a - b;
}
if (a % 2) {
return -1;
}
if (b % 2) {
return 1;
}
return a - b;
});
// concat() method. Combine arrays, but not depth Merge
var a = [1, 2, 3];
a.concat(4, 5); // [1, 2, 3, 4, 5]
a.concat([4, 5]); // [1, 2, 3, 4, 5]
a.concat([4, 5], [8, 9]); // [1, 2, 3, 4, 5, 8, 9]
a.concat([4, 5], [6, [10, 19]]); // [1, 2, 3, 4, 5, 6, [10, 19] ]
// slice() method. The source array does not change.
var a = [1, 2, 3, 4, 5];
a.slice(0, 3); // [1, 2, 3]
a.slice(3); // [4, 5];
a.slice(1, -1); // [2, 3, 4]
a.slice(1, -1 5)
a.slice(1, 4);
a.slice(-3, -2); // [3]
a.slice( -3 5, -2 5);
a.slice(2, 3);
/* Note:
does not include the element specified by the second parameter.
Negative values are converted to: negative Value array length
*/
// splice(pos[, len[, a, b]]) method. After deleting the specified position, specify the length element, and then append the element;
// Return an array composed of deleted elements. The original array is changed.
var a = [1, 2, 3, 4, 5, 6, 7, 8];
a.splice(4); // [ 5, 6, 7, 8]; at this time a: [1, 2, 3, 4]
a.splice(1, 2); // [2, 3]; at this time a: [1, 4 ];
a.splice(1, 1); // [4]; At this time a: [1];
var a = [1, 2, 3, 4, 5];
a.splice(2, 0, 'a', 'b'); // [1, 2, 'a', 'b', 3, 4, 5]
a.splice(2, 2 , [1, 2], 3); // ['a', 'b']; At this time a: [1, 2, [1, 2], 3, 3, 4, 5]
/* Note:
The parameters after the second parameter are directly inserted into the processing array.
The first parameter can be a negative number.
*/
// push() method and pop() method.
// push() can convert one or more appends a new element to the end of the array, and then returns the new length of the array;
// pop() deletes the last element in the array, reduces the length of the array, and returns the value it deleted.
// Note: Two Each method modifies the original array instead of generating a modified copy of the array.
var stack = [];
stack.push(1, 2); // stack: [1, 2 ]; return 2;
stack.pop(); // stack: [1]; return 2; deleted element value
stack.push(3); // stack: [1, 3]; return 2;
stack.pop(); // stack: [1]; return 3; deleted element value
stack.push([4, 5]); // stack: [1, [4, 5]]returm 2;
stack.pop(); // stack: [1]; return [4, 5]; deleted element value
// unshift() method and shift() Method. Same as above, starting from the head of the array.
// toString() method and toLocalString()
[1, 2, 4].toString(); // 1,2,3 ;
['a', 'b', 'c'].toString(); // 'a,b,c';
// Same as the join method without parameters.
/* New jsapi methods: map, every, some, filter, forEach, indexOf, lastIndexOf, isArray */
/* Array-like object*/
arguments
document.getElementsByTagName();

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

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

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

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

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

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

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

Python中如何使用__add__()函数定义两个对象的加法运算在Python中,可以通过重载运算符来为自定义的对象添加对应的运算功能。__add__()函数是用于定义两个对象的加法运算的特殊方法之一。在本文中,我们将学习如何使用__add__()函数来实现对象的加法运算。在Python中,可以通过定义一个类来创建自定义的对象。假设我们有一个叫做"Vect


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver CS6
Visual web development tools
