Object-Oriented Fundamentals

Source: Internet
Author: User

1. Object description

An object is a special type of data that is encapsulated by attributes and methods

Property refers to the value associated with the object: the name of the object. Property name

A method refers to the behavior that an object can perform or a function that can be accomplished: the object name. Method Name ()

Defining objects

Create an instance of an object

Create a template for an object

Using JSON

2. Create a Generic Object

To create a common object by using object objects

function Testobject () {varpersonobj=NewObject (); //Add PropertyPersonobj.name="John"; Personobj.age= -; //Add Methodpersonobj.say=NewFunction ("alert (' hello! ');"); //TestPersonobj.say (); alert (personobj.age);}

3. Creating templates for objects

Define constructors to create custom objects

Syntax: function objname (parameter 1, parameter 2,...) {}

function Person (n,a) {//defining the name and age properties     This. name=N;  This. age=A; //Definition Method ShowName     This. showname=function () {alert ("My Name is"+ This. Name);    }; //Definition Method Introduceself     This. introduceself =Introfunc ();} function Introfunc () {alert ( This. name+":"+ This. age);}
// Test Object function Test () {    var o1=New person ("Mary", );    alert (o1.age);    O1.showname ();    O1.introduceself ();         var o2=New person ("John",+);    alert (o2.age);    O2.showname ();    O2.introduceself ();}

4. JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format

Define by using name/value pairs

The name needs to be caused by ""

Use between many pairs of definitions, separating

The name can be a property

The property value of the string type, which needs to be caused by the ""

var obj={    "name":"Jerry",     "Age":; // test Object alert (obj.name);

Object-Oriented Fundamentals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.