javascript的object和function的基础知识
随手记录一些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";
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();
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
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.
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
})();
参见:https://www.w3schools.com/js/js_function_call.asp
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.//第一种写法用了:Self-Invoking Functions // 同时注意incrementCounter可以访问counter变量 var testModule = (function () { var counter = 0; return { incrementCounter: function () { return ++counter; }, resetCounter: function () { console.log( "counter value prior to reset: " + counter ); counter = 0; } }; })(); console.log("testModule type:"+typeof(testModule)); // Increment our counter testModule.incrementCounter(); testModule.resetCounter(); // 第二种写法,注意第一种写法的在function外加一个()并加上();结尾的写法。 // 如果没有这个写法,直接用testModule2.incrementCounter()会报错,因为testModule2是一个function var testModule2 = function () { var counter = 0; return { incrementCounter: function () { return ++counter; }, resetCounter: function () { console.log( "counter value prior to reset: " + counter ); counter = 0; } }; }; var testModule2_ins = testModule2(); console.log("testModule2 type:"+typeof(testModule2)); console.log("testModule2() type:"+typeof(testModule2_ins)); testModule2_ins.incrementCounter(); testModule2_ins.resetCounter();
上面的程序还用到了闭包closures的概念,参见js闭包篇。
example2:这种写法也很有代表性:https://jsfiddle.net/hotae160/
参考:https://www.w3schools.com/js/js_object_constructors.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects