当前位置:网站首页>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();
边栏推荐
- ZABBIX API batch delete a template of the host
- Take you to master the formatter of visual studio code
- Johnson–Lindenstrauss Lemma
- Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
- [untitled]
- Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
- Global and Chinese market of digital impression system 2022-2028: Research Report on technology, participants, trends, market size and share
- The boss said: whoever wants to use double to define the amount of goods, just pack up and go
- The difference between lambda expressions and anonymous inner classes
- Format character%* s
猜你喜欢
MySQL workbench use
Override and virtual of classes in C #
Idea if a class cannot be found, it will be red
C # learning notes: structure of CS documents
Kiss number + close contact problem
VRRP+BFD
LV1 previous life archives
17. File i/o buffer
Yyds dry goods inventory it's not easy to say I love you | use the minimum web API to upload files
ZABBIX API pulls the values of all hosts of a monitoring item and saves them in Excel
随机推荐
Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?
Reading notes - learn to write: what is writing?
LV1 Roche limit
Optimization theory: definition of convex function + generalized convex function
WP collection plug-in free WordPress collection hang up plug-in
What are the main investment products of bond funds and what are they
查詢效率提昇10倍!3種優化方案,幫你解决MySQL深分頁問題
Override and virtual of classes in C #
17. File i/o buffer
Global and Chinese market for travel wheelchairs 2022-2028: Research Report on technology, participants, trends, market size and share
Talking about custom conditions and handling errors in MySQL Foundation
Hamburg University of Technology (tuhh) | intelligent problem solving as integrated hierarchical reinforcement learning
How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #
12. Gettimeofday() and time()
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
A fan summed up so many interview questions for you. There is always one you need!
基於.NetCore開發博客項目 StarBlog - (14) 實現主題切換功能
Chain ide -- the infrastructure of the metauniverse
Gee import SHP data - crop image
I stepped on a foundation pit today