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.