当前位置:网站首页>JS object definition
JS object definition
2022-07-04 02:38:00 【ysds20211402】
from : Micro reading https://www.weidianyuedu.com
Method 1 : adopt var object={} Object literal
(1), You can call the object direct quantity to customize an object . Object self face quantity is an expression , Each operation of this expression creates and initializes a new object .
var user = { x:1,y:2,z:3,pp:function(){ alert("22222222") }}
// Define an object literally , It is saved in the form of name value pairs
alert(user.y) // Call the property
user.pp() // Calling method
(2) var student = { // You can also wrap , Define each attribute name and value , Comma off , The last one without a comma , If you add it, it will be ignored , But in ie China will report an error , Better not add
"x":1, // When defining the attribute name, you can use double quotation marks
'y':2, // When defining attribute names, you can use single quotation marks
z:12.3, // You can add any... When defining the attribute name
"for":" Use keywords to define attribute names. Use quotation marks ", // If you use a keyword definition, you must use double quotation marks man:{ // Objects can also be checked in
text:null, // The attribute value of an object can be a variety of data types
text2:undefined,
married:true
},
dd:function(){alert("33333333")}
}
alert(student.x)
student.dd()
Method 2 : adopt var obj = new Object(); Create objects
1、 Create a blank Object object
How to write it 1:var obj = new Object();
How to write it 2:var obj = {};
2、 Add properties and methods to the created object
var obj = {};
obj.name = ' Xiao Ming '; // attribute
obj.showName = function(){ // Method
alert(this.name);
};
obj.showName(); // Method call
==========================================
1 <script>
2 var obj = new Object() // Created an object ,object Objects are also top-level objects in objects
3 var arry = new Array() // Created an array object
4 var date = new Date() // Create a time object
5 var reg = new RegExp("js") // Create a regular object 6
</script>
==========================================
===========================================
adopt object.create To create objects
1 <script>
2 var obj = Object.create({x:1})
3 var obj = Object.create(null) // Created an object without a prototype , There's no way
4 var obj = Object.create(obj.prototype) // Created an ordinary empty object
5 </script>
===========================================
Method 3 : Create objects through constructors
adopt instanceof Operator can detect whether an object is created by a specified constructor function
Be careful :
It can be used through new Operator gets the object .
When you create an object with a constructor, you can receive parameters
The first letter of constructor function should be capitalized , Different from other general functions
function Person(name,age){ // Create a person Function of
this.name =name; m.weidianyuedu.com // Here this Corresponding to the object obj
this.age = age
}
var obj = new Person("ziksang",21) // Using a constructor to create an object
alert(obj.name) //=>ziksang Use the object name . Object to get the attribute value
alert(typeof obj) //=> use typeof To detect obj Is it an object type , return object
alert(obj instanceof Person) //=> use instanceof To detect objcet Is it right? person The object constructed by function , return true
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Method (1)== Method (2)
(1)
var obj = new Object();
obj.name = " The Monkey King ";
obj.age = 18;
obj.sayName = function(){ alert(obj.name); };
function fun(){ alert(obj.name); };
obj.sayName();
fun();
(2)
var obj2 = {
name:" Pig eight quit ",
age:18,
sayName:function(){ alert(obj2.name); }
};
obj2.sayName();
边栏推荐
- I stepped on a foundation pit today
- PTA tiantisai l1-079 tiantisai's kindness (20 points) detailed explanation
- Keep an IT training diary 055- moral bitch
- What are the main investment products of bond funds and what are they
- Create real-time video chat in unity3d
- MySQL advanced (Advanced) SQL statement (I)
- Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
- The automatic control system of pump station has powerful functions and diverse application scenarios
- Measurement fitting based on Halcon learning [4] measure_ arc. Hdev routine
- Ai aide à la recherche de plagiat dans le design artistique! L'équipe du professeur Liu Fang a été embauchée par ACM mm, une conférence multimédia de haut niveau.
猜你喜欢
WordPress collection WordPress hang up collection plug-in
Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?
What are the conditions for the opening of Tiktok live broadcast preview?
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
C # learning notes: structure of CS documents
C language black Technology: Archimedes spiral! Novel, interesting, advanced~
長文綜述:大腦中的熵、自由能、對稱性和動力學
Life cycle of instance variables, static variables and local variables
Redis transaction
Unity knapsack system (code to center and exchange items)
随机推荐
Libcblas appears when installing opencv import CV2 so. 3:cannot open shared object file:NO such file or directory
Unspeakable Prometheus monitoring practice
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
Contest3145 - the 37th game of 2021 freshman individual training match_ E: Eat watermelon
Site favorites
求esp32C3板子連接mssql方法
What is the intelligent monitoring system of sewage lifting pump station and does it play a big role
[Yugong series] February 2022 attack and defense world advanced question misc-83 (QR easy)
Take you to master the formatter of visual studio code
From the 18th line to the first line, the new story of the network security industry
Www 2022 | taxoenrich: self supervised taxonomy complemented by Structural Semantics
The requests module uses
FRP intranet penetration
Create template profile
Yyds dry goods inventory hand-in-hand teach you the development of Tiktok series video batch Downloader
Write the first CUDA program
I stepped on a foundation pit today
13. Time conversion function
Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)
VRRP+BFD