This article summarizes the way js creates objects. Share it for your reference. The details are as follows:
The code copy is as follows:
<script type="text/javascript">
//1. Create an object through literals
var obj = {hobby:'basketball',girlf:'beauty'};
document.write(obj.hobby + ' and ' + obj['girlf']);
//2. Create objects through new Object, which is the system's own constructor
var obj2 = new Object({hobby:'basketball',girlf:'beauty'});//If no parameters are added, an empty object is returned. This creation method is basically not used
//3. The method of self-constructor is consistent with the second method. The difference is that the second type of Object is built on the system, and it is created here for itself.
function Dog(){
this.leg = 4;
this.bark = function(){
alert('Wow...');
};
}
var obj3 = new Dog();
alert(obj3.leg);
</script>
I hope this article will be helpful to everyone's JavaScript programming.