How JavaScript creates an object summary (4 ways) _javascript Tips

Source: Internet
Author: User

The example in this article summarizes the way JavaScript creates objects. Share to everyone for your reference, specific as follows:

Subconsciously, JavaScript is not an object-oriented language, it can only be said to be an object-oriented language, at least it is not good to match the three basic object-oriented characteristics (inheritance, encapsulation, polymorphism), of course, many people think that JavaScript is an object-oriented language, It seems to be true, because object-oriented can also be implemented in JavaScript, such as inheritance, encapsulation bar can also be implemented in JavaScript, but easy to implement? So I feel very puzzled. See the Internet has a netizen evaluation is very good, "object-oriented is just a thought, language can only say whether it is good to support object-oriented features." "If you have a certain understanding of object-oriented, and C can also write object-oriented programs, JavaScript is also the case." So say can not assert that JavaScript is object-oriented language, hehe, think it is a rookie, dare not jump from the assertion, or look at the code:

1. Creating Objects with JSON

var company = {};
Company.name= ' Huawei ';
company.address = ' Beijing ';
Company.produce = function (message)
{
 alert (message);
}

2. Using the object type in JavaScript

company= new Object ();
Company.name= ' Taobao ';
company.address = ' Hangzhou ';
company.produce= function (message)
{
 alert (message);
}

3. Create a function to generate an object

Company = function ()
{
  this.name = ' sina ';
  this.address = ' Beijing ';
  This.produce = function (message)
  {
  alert (message);
  }
}

4. Using the browser Window object

Window.name = ' Tencent ';
window.address = ' Beijing ';
Window.produce = function (message)
{
  alert (message);
}

Extended:

1. Object replication

Emptyobject = new Object ();
company.apply = function (o, C,)
{
  if (o && c && typeof c = = ' object ')
  {for
  (Var p in c)
  {
  O[p] = c[p];
  }
  return o;
};
Emptyobject = ext.apply (Emptyobject,company);

2. Object replication (function method)

var copyoo = new Function ();
Copyoo.prototype = Company;
var newcopyoo = new Copyoo ();

I hope this article will help you with JavaScript programming.

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.