Xiaoqiang's HTML5 mobile development road (27) - JavaScript review 2

黄舟
Release: 2017-02-04 14:16:14
Original
1236 people have browsed it

JavaScript object-oriented basic knowledge

1. How to define a class and use the following syntax to create a class

function Person(name, age){ //习惯上第一个字母大写 //this修饰的变量称为属性 this.name = name; this.age = age; //如果属性值是一个函数,则这个属性就是一个方法 this.play = function(){ alert('play football...'); }; }
Copy after login

2. How to create an instance of the class

var p = new Person('zs', 22); p.play(); p.name;
Copy after login

3. Two other ways to create javascript objects

(1) First create an instance of the Object type, and then add new properties and methods

javascript is a Dynamic language, you can add new properties and methods to objects at runtime

var obj = new Object(); obj.name = 'zs'; obj.age = 22; obj.play = function(){ alert('play...'); };
Copy after login

(2) Use "json" syntax

var p = {'name':'zs','age':22}; ar p = {'name':'zs','play':function(){ alert('hello'); };
Copy after login

or

var p = {name:'zs',age:22,marrid:false}; var p = {name:'zs',play:function(){ alert('hello'); }};
Copy after login

attribute value If it is a string, it must be enclosed in quotation marks (single and double)

The attribute value allows number, string, boolean, null, Object

var p = {name:'zs', address:{ city:'beijing', street:'ca' } };
Copy after login

A complete example

   
Copy after login


The above is the content of Xiaoqiang’s HTML5 mobile development road (27) - JavaScript review 2. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!