


Factory pattern:
Junior developers may define objects like this:
var obj = new Object();
obj.name = "hero";
obj.showName=function (){alert( this.name);}
A problem here is that if we want to use the obj object in multiple places, we may have to write similar code many times in the program, so the factory method
function createObj()
{
var obj = new Object();
obj.name="hero";
obj.showName=function (){alert(this.name);}
return obj;
}
var obj1 = createObj();
var obj2 = createObj();
and another method constructor method
function obj (name)
{
this.name=name;
this.showName= function (){alert(this.name);}
}
Encapsulate the code that generates the object to avoid duplicating new code. Of course, you can also A further improvement is to pass some parameters to createObj instead of assigning a default fixed value to obj:
function createObj(name)
{
var obj = new Object();
obj.name=name;
obj.showName=function (){alert(this.name);}
return obj;
}
var obj1 = createObj("hero");
var o'b' j2=createObj("dby");
But there is a problem, that is, every time we call the createObj function, a new function showName will be created. This means that each object has its own version of showName, so we need to improve to avoid this problem. .
function showName()
{
alert(this.name)
}
function createObj(name)
{
var obj = new Object();
obj.name=name;
obj.showName=showName;
return obj;
}
This solves the problem of repeatedly creating functions, hahaha, you’re done.
Prototype method:
Mainly use the prototype attribute of the object.
function obj()
{}
obj.prototype.name="hero";
obj.prototype.showName=function()
{
alert(this.name);
}
It seems to be more perfect than the factory just now, but there is a problem. This function has no constructor and the attributes are specified through prototype. This is In practical applications, it is very troublesome. It is really unacceptable that the attributes of all instances are the same. In particular, there is a security risk, that is, when there are references in the object, for example, adding such a paragraph
obj.prototype. nameArray = new Array("hero","dby");
Then
obj1 = new obj();
obj2 = new obj();
obj1.nameArray.push("lxw" );
You will also see this attribute in the nameArray of obj2, because the nameArray of the two objects points to the same reference.
So this method is not ideal.
Needs improvement
Combine the constructor, define properties in the constructor, and use prototype definition methods
For example,
fuction obj(name)
{
this.name = name
this.nameArray = new Array(" hero","dby");
}
obj.prototype.showName = function(){alert(this.name)}
All non-function properties are created in the constructor, and function properties are created using Created in prototype mode, changing the nameArray value in obj1 will not affect the nameArray value of the obj2 object, and there is only one showName function, so there is no memory waste.
Basically perfect, the rest are basically other things Modified. If you are interested, you can change it and play with it yourself.
The author added a singleton here to play with:
function obj (name)
{
this.name = name;
this.nameArray=new Array("hero","dby");
if(typeof obj._initialized=='undefined')
{
obj.prototype.showName=function(){ alert(this.name);}
obj._initialized="true";
}
}
In fact, it is not a singleton. It is just that when constructing an object, it first determines whether an attribute is defined. If it is not defined, then use the prototype method to continue defining the object. If the attributes have been defined, then the function will not be defined again. The prototype method is only created once and assigned once.
It’s almost perfect again, and you’re done. .
This is my personal understanding, and I hope it will be helpful to everyone. If there are any imperfections, please contact QQ and correct them in time.
This is a complete example:
function Rectangle(name,color,width,heigth){
this.name=name;
this.color=color;
this.width= width;
this.heigth=heigth;
}
Rectangle.prototype.area=function(){
return this.width*this.heigth
}
Rectangle.prototype. show=function(){
document.write(this.name " " this.color " " this.width " " this.heigth "
");
document.write(this.area() );
}
var obj1= new Rectangle("Rectangle","red",15,20);
obj1.show();

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
