Home >Web Front-end >JS Tutorial >javascript study notes (8) javascript object_basic knowledge

javascript study notes (8) javascript object_basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:08:03888browse
1. Built-in objects in JavaScript
In addition to its own built-in objects in JavaScript, there are the following objects that we are familiar with:
•Array
•Date
•Math
•String
•RegExp
•……
Each object has its own attributes and methods. For example, we often use attributes and methods
attributes: stringObject.length; arrayObject.length;……
Methods: stringObject.indexOf(); stringObject.splite(); stringObject.substr(); arrayObject.concat(); arrayObject.push(); arrayObject.join();......
2. How Customize objects, and add properties and methods
a. Create

var newObject = new Object(); //Create a new class
newObject.name = "new object"; //Add a name attribute
newObject.say = function() { //Add say() method
alert(this.name); //output new object
}

For the above creation method, we can use JSON( JavaScript Object Notation) method is abbreviated as the following code:
Copy code The code is as follows:

var newObject = {
name: "new object";
say: function () {
alert(this.name);
}
};

We use the JSON data format to create a more complex object
Copy code The code is as follows:

var company = {
name: "tuanzz",
product: "groupon",
address: {province: "Hubei", city: "wuhan"},
person:[
{name: "zhangchen",age: "23"},
{name: "luomi", age: "23"},
],
readme: function() {
alert ("My name is " this.person[0].name " and " this.person[0].age " years old");
}
};
company.readme(); / /output My name is zhangchen and 23 years old;

We can see that the code of objects created using JSON data format not only looks very elegant.
The format of JSON is a list of items enclosed in braces "{}", each item is separated by a comma ",", and the item is an attribute name and attribute value separated by a colon ":". This is a typical dictionary representation, and it once again shows that objects in JavaScript are dictionary structures. No matter how complex the object is, it can be created and assigned with a JSON code.
b. Create objects through constructors
Copy code The code is as follows:

function objectFun(name) {
this.name = name;
this.say = function() {
alert(this.name);
}
}
var newObject = new objectFun("zhangchen");
newObject.say(); //output zhangchen

First create a new objectFun() function, which defines the properties and methods. Here we can see objectFun Make a class (a function is an object in JavaScript), and then instantiate an object through new. The newObject object also has the properties and methods of the parent class.
We can use the following code to detect that the function is indeed an object:
Copy the code The code is as follows:

//Ordinary function
function say(s) {
alert(s);
}
say("hi");
//Assign attributes to the function object, function It's the object
say.test = "it can work?";
alert(say.test); //output it can work?

How to understand the above method of creating an object? Let’s look at the following code:
Copy code The code is as follows:

function objectFun(name) {
this.name = name;
this.say = function() {
alert(this.name);
}
}
var newObject = new Object(); / /Create an empty object
objectFun.call(newObject, "zhangchen"); //Call the objectFun function using newObject as this parameter
newObject.say("zhangchen");//output zhangchen

먼저 newObject 객체를 생성하고 newObject를 이 매개변수로 사용하여 objectFun 함수를 호출합니다. 많이 말했지만, 우리는 objectFun을 생성자로 완전히 사용할 수 있습니다.
---------------------------------- --- ---------------------------------- --- -------------------
나머지 내용은 Li Zhan이 작성한 JavaScript 이해를 참조하세요.
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