随手记录一些javascript的一些基础知识,之前只是简单用到javascript,并没有了解其中的概念。
1. Javascript Object:
In JavaScript, almost "everything" is an object.
- Booleans can be objects (if defined with the
new
keyword) - Numbers can be objects (if defined with the
new
keyword) - Strings can be objects (if defined with the
new
keyword) - Dates are always objects
- Maths are always objects
- Regular expressions are always objects
- Arrays are always objects
- Functions are always objects
- Objects are always objects
All JavaScript values, except primitives, are objects.
1.1 Object Methods
Methods are actions that can be performed on objects.
Object properties can be both primitive values, other objects, and functions.
An object method is an object property containing a function definition.
Property | Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
fullName | function() {return this.firstName + " " + this.lastName;} |
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue"; b) Using an Object Literal const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
1.3 JavaScript Object Methods
JavaScript methods are actions that can be performed on objects. 注意这里的用词是method,而不是function。
A JavaScript method is a property containing a function definition.
Property | Value |
---|---|
firstName | John |
lastName | Doe |
age | 50 |
eyeColor | blue |
fullName | function() {return this.firstName + " " + this.lastName;} |
let x = message.toUpperCase(); 1.4 Object Constructors
Sometimes we need a "blueprint" for creating many objects of the same "type".
The way to create an "object type", is to use an object constructor function.
In the example above, function Person()
is an object constructor function.
Objects of the same type are created by calling the constructor function with the new
keyword:
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");
A function designed to create new objects, is called an object constructor. 1.5 Object Prototypes 对于上面的Persion这个constructor function,you can not add a new property to an existing object constructor,例如这样是错误的:Person.nationality = "English"; 但你可以在实例化的object中添加一个property,如果你需要对这个模版的函数统一增加一个属性,可以用prototypes:
All JavaScript objects inherit properties and methods from a prototype:
Date
objects inherit fromDate.prototype
Array
objects inherit fromArray.prototype
Person
objects inherit fromPerson.prototype
The Object.prototype
is on the top of the prototype inheritance chain:
Date
objects, Array
objects, and Person
objects inherit from Object.prototype
.
The JavaScript prototype
property allows you to add new properties to object constructors:
let x = "Hello!!"; // I will invoke myself
})(); 2.3 call(), apply() 方法:
参见:https://www.w3schools.com/js/js_function_call.asp 2.4 bind()方法: With the
bind()
method, an object can borrow a method from another object. 参见 https://www.w3schools.com/js/js_function_bind.asp ,但我的理解和这里描述的不同,我的理解是method把自己的instance bind到bind中的参数了,其中的例子中的member并没有新增fullName method.
TODO: 还有一个大点是JS中this的用法,这个很灵活,也很重要,参见:https://www.w3schools.com/js/js_this.asp ,有机会我再写下我的体会。
标签:function,javascript,object,基础知识,Object,objects,new,一些,method From: https://www.cnblogs.com/saaspeter/p/17129832.html