topic:
try{
var me = Man({ fullname: "Little Red" });
var she = new Man({ fullname: "Little Red" });
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiaohong
My gender is: <User not entered>
------------------*/
("fullname", "Xiao Ming");
("gender", "male");
= "waste";
= "Shemale";
("gender", "female");
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiao Ming
My gender is: male
------------------*/
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiaohong
My gender is: Female
------------------*/
({
"words-limit": 3,
"words-emote": "Smile"
});
("I like watching videos.");
("Our office is so beautiful.");
("There are so many beauties in the video!");
("I usually watch Youku!");
();
(());
/*------[Execution result]------
Xiao Ming smiled: "I like watching videos. Our office is so beautiful. There are so many beauties in the video!"
------------------*/
({
"words-limit": 2,
"words-emote": "Scream"
});
(());
();
/*------[Execution result]------
Xiao Ming shouted: "I like watching videos. Our office is so beautiful."
------------------*/
}catch(e){
("Execution error, error message: " + e);
}
Knowledge points:
(1) JS object-oriented basics: ECMA-262 defines an object as: "a collection of unordered attributes, whose attributes can contain basic values, objects or functions."
(2) Methods for creating objects in JS:
(a) Factory pattern: Use functions to encapsulate the details of creating objects with specific interfaces.
function createPerson(name, age, job){
var o = new Object();
= name;
= age;
= job;
= function(){
alert();
};
return o;
}
var person1 = createPerson(“Nicholas”, 29, “Software Engineer”);
var person2 = createPerson(“Greg”, 27, “Doctor”);
Disadvantages: Although the factory pattern solves the problem of creating multiple acquainted objects, it does not solve the problem of object recognition (i.e. how to know the type of an object).
(b) Constructor mode: Constructors in ECMAScript can be used to create objects of specific types. You can create custom constructors to define properties and methods of custom object types.
function Person(name, age, job){
= name;
= age;
= job;
= function(){
alert();
};
}
var person1 = new Person(“Nicholas”, 29, “Software Engineer”);
var person2 = new Person(“Greg”, 27, “Doctor”);
Disadvantages: The main problem with using constructors is that each method needs to be recreated on each instance. Don't forget - functions in ECMAScript are objects, so each function is defined,
It is to instantiate an object.
(c) Prototype pattern: Each function we create has a prototype property, which is a pointer, pointing to an object, and the purpose of this object is to contain a specific type that can be used by
All instances of shared properties and methods. The advantage of using a prototype object is that it allows all objects to share the properties and methods it contains
function Person(){
}
= “Nicholas”;
= 29;
= “Software Engineer”;
= function(){
alert();
};
var person1 = new Person();
(); //”Nicholas”
var person2 = new Person();
(); //”Nicholas”
alert( == ); //true
Disadvantages: All properties in the prototype are shared by many instances, and this sharing is very suitable for functions. However, for attributes that reference type values, the problem is more prominent.
(d) Combination of constructor patterns and prototype patterns: The most common way to create custom types is to use combination of constructor patterns and prototype patterns. The constructor mode is used to define instance properties.
And the prototype pattern is used to define methods and shared properties.
function Person(name, age, job){
= name;
= age;
= job;
= [“Shelby”, “Court”];
}
= {
constructor: Person,
sayName : function () {
alert();
}
};
var person1 = new Person(“Nicholas”, 29, “Software Engineer”);
var person2 = new Person(“Greg”, 27, “Doctor”);
(“Van”);
alert(); //”Shelby,Court,Van”
alert(); //”Shelby,Court”
alert( === ); //false
alert( === ); //true
Answer:
<!DOCTYPE html>
<html>
<head>
<style type="text/css" rel="stylesheet">
</style>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
=function()
{
var Man;
//++++++++++++++++++ Answering area++++++++++++++++++++++
Man=function(obj){
if(!(this instanceof Man))
{
return new Man(obj);
}
=obj||{};
=[];
}
={
constructor:Man,
words:function(word){
word!=undefined&&(word);
},
attr:function(attribute,attributeValue)
{
var defaultVaule="<User not entered>";
if(==2){
[attribute]=attributeValue;
}
else if(!(attribute instanceof Object))
{
if(([attribute]===undefined))
{
return defaultVaule;
}
else
{
return [attribute];
}
}
else{
for(property in attribute)
{
[property]=attribute[property];
}
}
},
say:function()
{
var limit=['words-limit'],
outputString,
wordsLen=;
outputString=("fullname")+("words-emote")+":";
for(var i=0;i<limit;i++)
{
outputString+=[i];
}
return outputString;
}
};
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
try{
var me = Man({ fullname: "Little Red" });
var she = new Man({ fullname: "Little Red" });
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiaohong
My gender is: <User not entered>
------------------*/
("fullname", "Xiao Ming");
("gender", "male");
= "waste";
= "Shemale";
("gender", "female");
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiao Ming
My gender is: male
------------------*/
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiaohong
My gender is: Female
------------------*/
({
"words-limit": 3,
"words-emote": "Smile"
});
("I like watching videos.");
("Our office is so beautiful.");
("There are so many beauties in the video!");
("I usually watch Youku!");
();
(());
/*------[Execution result]------
Xiao Ming smiled: "I like watching videos. Our office is so beautiful. There are so many beauties in the video!"
------------------*/
({
"words-limit": 2,
"words-emote": "Scream"
});
(());
();
/*------[Execution result]------
Xiao Ming shouted: "I like watching videos. Our office is so beautiful."
------------------*/
}catch(e){
("Execution error, error message: " + e);
}
}
</script>
</html>
Copy the codeThe code is as follows:
try{
var me = Man({ fullname: "Little Red" });
var she = new Man({ fullname: "Little Red" });
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiaohong
My gender is: <User not entered>
------------------*/
("fullname", "Xiao Ming");
("gender", "male");
= "waste";
= "Shemale";
("gender", "female");
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiao Ming
My gender is: male
------------------*/
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiaohong
My gender is: Female
------------------*/
({
"words-limit": 3,
"words-emote": "Smile"
});
("I like watching videos.");
("Our office is so beautiful.");
("There are so many beauties in the video!");
("I usually watch Youku!");
();
(());
/*------[Execution result]------
Xiao Ming smiled: "I like watching videos. Our office is so beautiful. There are so many beauties in the video!"
------------------*/
({
"words-limit": 2,
"words-emote": "Scream"
});
(());
();
/*------[Execution result]------
Xiao Ming shouted: "I like watching videos. Our office is so beautiful."
------------------*/
}catch(e){
("Execution error, error message: " + e);
}
Knowledge points:
(1) JS object-oriented basics: ECMA-262 defines an object as: "a collection of unordered attributes, whose attributes can contain basic values, objects or functions."
(2) Methods for creating objects in JS:
(a) Factory pattern: Use functions to encapsulate the details of creating objects with specific interfaces.
function createPerson(name, age, job){
var o = new Object();
= name;
= age;
= job;
= function(){
alert();
};
return o;
}
var person1 = createPerson(“Nicholas”, 29, “Software Engineer”);
var person2 = createPerson(“Greg”, 27, “Doctor”);
Disadvantages: Although the factory pattern solves the problem of creating multiple acquainted objects, it does not solve the problem of object recognition (i.e. how to know the type of an object).
(b) Constructor mode: Constructors in ECMAScript can be used to create objects of specific types. You can create custom constructors to define properties and methods of custom object types.
function Person(name, age, job){
= name;
= age;
= job;
= function(){
alert();
};
}
var person1 = new Person(“Nicholas”, 29, “Software Engineer”);
var person2 = new Person(“Greg”, 27, “Doctor”);
Disadvantages: The main problem with using constructors is that each method needs to be recreated on each instance. Don't forget - functions in ECMAScript are objects, so each function is defined,
It is to instantiate an object.
(c) Prototype pattern: Each function we create has a prototype property, which is a pointer, pointing to an object, and the purpose of this object is to contain a specific type that can be used by
All instances of shared properties and methods. The advantage of using a prototype object is that it allows all objects to share the properties and methods it contains
function Person(){
}
= “Nicholas”;
= 29;
= “Software Engineer”;
= function(){
alert();
};
var person1 = new Person();
(); //”Nicholas”
var person2 = new Person();
(); //”Nicholas”
alert( == ); //true
Disadvantages: All properties in the prototype are shared by many instances, and this sharing is very suitable for functions. However, for attributes that reference type values, the problem is more prominent.
(d) Combination of constructor patterns and prototype patterns: The most common way to create custom types is to use combination of constructor patterns and prototype patterns. The constructor mode is used to define instance properties.
And the prototype pattern is used to define methods and shared properties.
function Person(name, age, job){
= name;
= age;
= job;
= [“Shelby”, “Court”];
}
= {
constructor: Person,
sayName : function () {
alert();
}
};
var person1 = new Person(“Nicholas”, 29, “Software Engineer”);
var person2 = new Person(“Greg”, 27, “Doctor”);
(“Van”);
alert(); //”Shelby,Court,Van”
alert(); //”Shelby,Court”
alert( === ); //false
alert( === ); //true
Answer:
Copy the codeThe code is as follows:
<!DOCTYPE html>
<html>
<head>
<style type="text/css" rel="stylesheet">
</style>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
=function()
{
var Man;
//++++++++++++++++++ Answering area++++++++++++++++++++++
Man=function(obj){
if(!(this instanceof Man))
{
return new Man(obj);
}
=obj||{};
=[];
}
={
constructor:Man,
words:function(word){
word!=undefined&&(word);
},
attr:function(attribute,attributeValue)
{
var defaultVaule="<User not entered>";
if(==2){
[attribute]=attributeValue;
}
else if(!(attribute instanceof Object))
{
if(([attribute]===undefined))
{
return defaultVaule;
}
else
{
return [attribute];
}
}
else{
for(property in attribute)
{
[property]=attribute[property];
}
}
},
say:function()
{
var limit=['words-limit'],
outputString,
wordsLen=;
outputString=("fullname")+("words-emote")+":";
for(var i=0;i<limit;i++)
{
outputString+=[i];
}
return outputString;
}
};
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
try{
var me = Man({ fullname: "Little Red" });
var she = new Man({ fullname: "Little Red" });
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiaohong
My gender is: <User not entered>
------------------*/
("fullname", "Xiao Ming");
("gender", "male");
= "waste";
= "Shemale";
("gender", "female");
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiao Ming
My gender is: male
------------------*/
();
("My name is: " + ("fullname") + "\nMy gender is: " + ("gender"));
();
/*------[Execution result]------
My name is: Xiaohong
My gender is: Female
------------------*/
({
"words-limit": 3,
"words-emote": "Smile"
});
("I like watching videos.");
("Our office is so beautiful.");
("There are so many beauties in the video!");
("I usually watch Youku!");
();
(());
/*------[Execution result]------
Xiao Ming smiled: "I like watching videos. Our office is so beautiful. There are so many beauties in the video!"
------------------*/
({
"words-limit": 2,
"words-emote": "Scream"
});
(());
();
/*------[Execution result]------
Xiao Ming shouted: "I like watching videos. Our office is so beautiful."
------------------*/
}catch(e){
("Execution error, error message: " + e);
}
}
</script>
</html>