在JavaScript中,构造函数是一种特殊的函数,用于初始化对象。当使用new关键字调用构造函数时,它会创建一个新的对象实例,并将构造函数内部的this关键字绑定到这个新对象上。构造函数通常用于定义对象的属性和方法,以及创建对象的实例。
function Person(name, age) {
this.name = name;
this.age = age;
}
var person1 = new Person('Alice', 25);
function Person(name, age) {
this.name = name; // 实例成员
this.age = age; // 实例成员
}
// 静态成员
Person.species = 'Homo sapiens';
// 注意:
// 1.name和age是每个通过Person构造函数创建的对象实例所独有的实例成员。
// 2.species是Person构造函数本身的静态成员,它与任何特定的Person实例无关。
当创建Person的实例时,可以像下面这样访问实例成员:
var alice = new Person('Alice', 30);
console.log(alice.name); // 输出: Alice
console.log(alice.age); // 输出: 30
而静态成员可以通过构造函数本身来访问:
console.log(Person.species); // 输出: Homo sapiens
JavaScript内置构造函数是语言提供的一组预先定义的函数,用于创建和管理不同类型的对象。这些构造函数不仅提供了创建对象的快捷方式,还定义了对象的默认行为和属性。内置构造函数包括但不限于Object、Array、String、Number、Boolean、Date、RegExp等。
const obj = { name: 'Alice', age: 30 };
console.log(Object.keys(obj)); // ["name", "age"]
console.log(Object.values(obj)); // ["Alice", 30]
let obj1 = { name: 'zs', age: 20 }
let obj2 = { height: 180, weight: 80 }
// let obj3 = {}
// 作用:将 第2个参数、第3个参数,第.....个参数中的属性,合并到第1个参数中
// 语法一:
// Object.assign(obj3, obj1, obj2)
// console.log(obj3)
// 语法二:
// let obj = Object.assign({}, obj1, obj2)
// console.log(obj)
// 使用它,实现对象的拷贝
let obj = Object.assign({}, obj1)
// 拷贝之后,两个对象独立。(修改一个对象,另一个不受影响)
obj.name = 'lisi'
console.log(obj) // { name: 'lisi', age: 20 }
console.log(obj1) // { name: 'zs', age: 20 }
let nums = [1, 2, 3];
nums.forEach(function(item, index, arr) {
console.log(item * 2); // 分别输出 2, 4, 6
});
let nums = [1, 2, 3, 4, 5];
let evens = nums.filter(num => num % 2 === 0);
console.log(evens); // 输出 [2, 4]
let doubles = [1, 2, 3].map(x => x * 2);
console.log(doubles); // 输出 [2, 4, 6]
let words = ['Hello', 'world'];
let str = words.join(' ');
console.log(str); // 输出 "Hello world"
let array = [1, 2, 3, 4, 5];
let found = array.find(element => element > 3);
console.log(found); // 输出 4
let bool = [2, 4, 6].every(n => n % 2 === 0);
console.log(bool); // 输出 true
bool = [2, 4, 6, 7].some(n => n % 2 !== 0);
console.log(bool); // 输出 true
let array1 = [1, 2];
let array2 = [3, 4];
let combined = array1.concat(array2);
console.log(combined); // 输出 [1, 2, 3, 4]
let arr = [3, 1, 4, 1, 5, 9];
arr.sort();
console.log(arr); // 输出 [1, 1, 3, 4, 5, 9]
// 语法:arr.splice(位置, 删除的数量, 新增的元素, 新增的元素, ........)
let list = ['a', 'b', 'c', 'd'];
list.splice(1, 2, 'x'); // 删除从第1索引开始的2个元素,插入'x'
console.log(list); // 输出 ['a', 'x', 'd']
let reversed = ['one', 'two', 'three'].reverse();
console.log(reversed); // 输出 ['three', 'two', 'one']
let index = [1, 2, 3, 4].findIndex(value => value > 2);
console.log(index); // 输出 2
// 示例一:计算数组中所有数值的总和
const arr= [1, 2, 3, 4, 5];
const sum = arr.reduce((total, curr) => total+ curr, 0);
console.log(sum); // 输出: 15
// 示例二:将数组转换为一个对象,统计每个元素出现的次数
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];
const fruitCounts = fruits.reduce((total, curr) => {
total[curr] = (total[curr] || 0) + 1;
return total;
}, {});
console.log(fruitCounts); // 输出: { apple: 3, banana: 2, orange: 1 }
length:获取字符串长度,反映字符串中字符的数量。
let str = "Hello World";
console.log(str.length); // 输出 11
let str = "apple,banana,grape";
let arr = str.split(",");
console.log(arr); // ["apple", "banana", "grape"]
let str = "Hello World";
console.log(str.substring(1, 5)); // 输出 "ello"
console.log('hello'.includes('h')) // true
console.log('hello'.includes('ell')) // true
console.log('hello'.includes('ho')) // false
console.log('hello'.includes('h', 2)) // false 从2的位置往后数,看字符串中是否包含h
console.log('hello'.startsWith('h')) // true
console.log('hello'.startsWith('he')) // true
console.log('hello'.startsWith('hello')) // true
console.log('hello'.startsWith('e')) // false
console.log('hello'.startsWith('e', 1)) // true。从位置1开始算起,看这段字符串的开头是否是e
console.log('hello'.endsWith('llo')) // true
console.log('hello'.endsWith('ll', 4)) // true,4个长度,意思是先截取4个字符hell,然后看这4个字符的结尾是否是ll
let str = "hello world";
console.log(str.toUpperCase()); // 输出 "HELLO WORLD"
let str = "HELLO WORLD";
console.log(str.toLowerCase()); // 输出 "hello world"
console.log(' hello ') // hello
console.log(' hello '.trim()) // hello
console.log('hello'.search('e')) // 1
console.log('hello'.search('l')) // 2
console.log('hello'.search('a')) // -1
let str = "hello world hello again";
console.log(str.indexOf("hello")); // 输出 0
let str = "hello world hello again";
console.log(str.replace(/hello/g, "hi")); // 输出 "hi world hi again"
const number = 123.456;
console.log(number.toFixed(2)); // "123.46" 最后一位符合四舍五入