Using JavaScript to implement Observer mode

Source: Internet
Author: User
Tags constructor object model object object

Original source: http://www.codeproject.com/KB/scripting/Observer_Pattern_JS.aspx

A simple introduction to JavaScript

JavaScript is a prototype (prototype) scripting language (formerly known as LiveScript). Its syntax is loosely similar to the C language. The scripting language was developed by the Netscape community for navigator browsers. Like the C language, JavaScript itself has no constructors or destructors. The C language relies on the standard input/output library, while JavaScript relies on the hosting environment that executes it. This scripting language uses custom functions, which may be referred to as procedures, routines, or functions in other languages. web-based JavaScript is primarily used to interact with the DOM (the Document Object model) in a Web page to accomplish features that are not implemented using HTML alone. JScript is a scripting language that Microsoft has launched with JavaScript, which is used in Microsoft's IE browsers.

Create a custom object in JavaScript

Creating a new JavaScript object requires 2 steps. First, you need to create a function that is the name of the new class. This function is what we often say about constructors. Then, you must use the new operator, followed by the name of the object and some necessary parameters to create an instance of the object. The following code defines a person function and then uses the new operator to create an instance of person:

function Person(name, surname)
{
   this.name = name;
   this.surname = surname;
}

var salvo = new Person('Salvatore', 'Vetro');

The This keyword is an instance of the object you are currently executing, and therefore allows you to add or modify the properties of the object on the current object.

How do I add a method to an object?

In JavaScript, any object is created by calling the constructor that binds the stereotype property. The syntax for adding a new method is as follows:

customeObject.prototype.newMethodName = function;
//方法体
Person.prototype.Speak = function(){...};

If you add a method to the prototype attribute of an object, all instances created by the constructor of the object have this new method. Note that the prototype itself is also an object and is able to define properties and methods for it through object literal syntax (objects literal syntax):

function NewObject()
{
   alert("I am a new object.");
}

NewObject.prototype =
{
   alert1 : function(str){alert(str);}, //新方法
   name : 'As you want', //新属性
   Alert2 : function(){alert('Bye.');}, //新方法
};
var newObject = new NewObject();
newObject.alert1("Ciao");
newObject.name;
newObject.alert2();

Each time the script attempts to read/write the properties of the object, JavaScript searches for a specific order of attributes that match the name. The order is as follows:

L Use the value of this property if it is already assigned to the current object;

L Check the value of the prototype property of the object constructor if the specified property is not searched for in the current object;

L Look up the prototype chain until it finds a matching attribute (already assigned to it), otherwise the object object is always found. Therefore, if you change the value of the constructor's prototype property, and the value of the property is not overridden in an instance of the constructor, JavaScript returns the value of the object's current prototype property.

Related Article

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.