forked from panache-chinmay/javascriptDeccan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript6.js
More file actions
474 lines (311 loc) · 11.2 KB
/
Copy pathscript6.js
File metadata and controls
474 lines (311 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Tracking data using objects
// The object literal
/*
1) Javascript is basically considered as object oriented or object based programming
language
2) Basically everthing in javascript functions ,arrays ,booleans , strings are either
object or can be treated as object in javascript
3) A simple way to think of an object , objects basically have properties and
values
4) properties are something that belongs to an object and methods are something
that can be done to an object.
5) We have seen many examples of object till now for example array
[1,2,3] --This array is an object which have property length
6) objects also have methods which are used to do somethin in array or to
do with items in the array.(for example push method add item to the array)
7) You can create you own object,that is a part of advanced JavaScript.
8) In this tutorial we are using object to basically store data
(Till now we had studied only two way to store values in variables and in arrays)
var name = "Dave";
var grades = [80,90,78,89]
9) Javscript object allows you to store data in the form
key:value
property:value
keys are properties are like variable name and 'value' like the value of that variable,
so you can called object as a single item that holds multiple values
var student = { }; // this are object literals ---->
Note :- curly braces creates an object and [] creates an array.
*/
// program 1 a)
// var student = {
// name:"chinmay",
// age:28
// }
/* 1) Please note the key is not in quotes , key are same as variables so
so the rules applied to defined variable name should be same as defining keys inspect
objects
2) Values can be another String , Boolean , Number , Array or may be another object.
3) As we separate items in array by ',' we can similary separate properties and their
values in javascript with ''
4) Spaces are optional in objects but it good idea to place it on separate lines
to make it more readable
5) In addtion you should intend each line inside a object (2 or 4 spaces)
6) Indentation helps you to see that those lines belong to the object
7) Space after the colon ':' between properties and values is a personal preference
*/
//program 1 b)
// var student = {name:"chinmay",age:28}
//
//
// // creating a object literal to know basic information about a person.
//
// // program 1 c)
//
// var person = {
// name: 'Sarah',
// country: 'US',
// age:35,
// treehouseStudent:true,
// skills:[ 'Javascript','HTML',]
// }
// program 2 a)
//Accessing object properties
/*
note:- object allows you to store multiple values in one single variable.
Array uses index to access the array item , while the object uses key to access the object item
1)
var students = ['Diane','Alicia','Frankie','Sam'];
alert(students[0]);
2) Two different ways to access values inside the object in JavaScript.
a) objectName.propertyName (Dot notation)
or
b)objectName["propertyName"] (Bracket notation)
3) . notation is very common, as we can update the values of any propertyName
4) using . notation , we can also generate new property
*/
// program 2 a)
// var person = {
// name: 'Sarah',
// country: 'US',
// age:35,
// treehouseStudent:true,
// skills:[ 'Javascript','HTML','CSS']
// }
//Accessing value via dot notation
// console.log(person.name);
//
// // Accessing value via bracket notation
// console.log(person['name']);
//
// // Updating value of existing property 'name'.
// person.name = 'Deo';
//
// // Creating new property for person object.
// person.color = "Wheatish";
/*
1) Think of object as a package of variables in form of properties
inside the object
2) It helps to keep the relevant data into a easily accessible unit,which
basically simplifies data handling .
3)You can pass object as an argument to any function and return object from a
function (When you need to return a large set of data use object )
4)Return statement allows you to return only one value.
*/
// program 2 b
// var person2 = {
// name: 'Sarah',
// country: 'US',
// age:35,
// treehouseStudent:true,
// skills:[ 'Javascript','HTML','CSS']
// };
//
// function print(message2){
// var div = document.getElementById('chinmay');
// div.innerHTML = message2;
// }
//
// console.log(person2.name);
// var message = '<p> Hello , My name is '+ person2.name +' </p>';
// message += '<p> I live in the ' + person2.country + ' </p>';
// person2.name = 'Chinmay';
// message += '<p> But , I wish my name was '+ person2.name +' </p>';
// person2.age += 1;
// message += '<p> My age is now '+ person2.age +' </p>';
// message += '<p> I have '+person2.skills.length+' skills';
// message += person2.skills.join(', ')+ '</p>';
//
// print(message);
//program 3
// Using 'for in' to loop through object's properties
/*
1) There are lot of similarities between arrays and objects ,
Arrays basically stores those values in specific numeric positions in list
2) Objects stores multiple values which can be acessed using key or property name.
3) Because the array value have numeric index , a for loop provide easy way to
access each item in array.
4) you can access each value in object using a special type of object called as 'for in loop'
5) It basically loops thorugh each 'key' or 'property' name in the object.
// Basic syntax
// To print keys
for( var key in objectName){
// do something
console.log(key);
}
// To print values of properties.
for( var key in objectName){
// do something
console.log(objectName[key]);
}
6) for ,var and in are keyword, key name can be anything you like , object name is name of object.
7)This basically gives us the access to key name , you can use square bracket notation,
yo access the value. Infact the only way to access the value of properties object vai 'for in' loops
is via bracket notation
8) With dot notation it will look for exact property name as it cannot substitue like brackey notation.
TO ACCESS THE VALUE WE WANT TO USE SQUARE BRACKET NOTATION IN CASE OF FOR IN LOOP
*/
//
// var person2 = {
// name : 'Sarah',
// country:'US',
// age:35,
// treehouseStudent:true,
// skills:['JAVASCRIPT','HTML','CSS']
// }
//
// for(prop in person2){
// //console.log(prop , ': ',person2[prop])
// // console.log(prop , ': ',person2["skills"]);
// }
// program 4
// Mixing and matching arrays and objects
/*
1) Arrays are great , they are easy way to create ordered level development
2) Objects are also great , they also provide a easy way to write structured data
and can be access using property , name and value .
*/
// var questions = [
// {questions: 'How Many States are in United States?',answer:50},
// {questions: 'How Many Continents are there?',answer:7},
// {questions: 'How Many Legs does the insect have ?',answer:6}
// ];
//
// var correctAnswers = 0 ;
// var questions;
// var answer ;
// var response;
//
// function print(message){
// document.write(message);
// }
//
// for(var i = 0 ; i < questions.length ; i += 1){
// questions = questions[i].question;
// answer = questions[i].answer;
// response = prompt(question);
// response = parseInt(response)
// if( respone === answer){
// correctAnswers += 1
// }
//
// }
//
// html = "You got " + correctAnswers + "question(s) right.";
// print(html);
// program 5
// javascript object notation
/*
1)Javascript object provides you the structured way to store the data.
2)Object stores most of the information at one place and store them in form of key and value
for easy access
3) They are so structured that they become one of the model for data exchange on web called as
jason.
4) JASON is commonly used wih the technology called AJAX which is commonly used to exchange
information between server side and web browser.
5) For example using Ajax browser can request for list of photographs recently uploded on AJAX.
6) The information for those images is send in jason format which can be easily converted
to javascript object , which can be later used to display as image .
7) Give them all the examples of regres.in.
8) Values of an object can be another object.
9) Basically web browsers are very good in understanding the string . for example (HTML tags)
10) You must know everthing about jason , if you want to be successful front end developer.
*/
/*
More on objects
Objects are just like varaibles , but they contain many values
*/
// var car = {
// type:'Fiat',
// model:'500',
// color:'White'
// };
//
// document.write(car.type);
/* The values are written in name:value pairs
1) Name and values separted by pair
2) Javascript objects are containers for named values called properties and
values.
3) You define and create javascript object with object literals
4)
Spaces and linevreaks are important , An object can span over multiple lines
*/
// var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
//
// var person = {
// firstName:'chinmay',
// lastName: 'deshpande',
// age:89,
// eyeColor:'blue'
// };
//
//
// console.log(`My name is ${person.firstName} and my age is ${person.age}`);
// Object properties
/*
The name:value pairs in javascript objects is calles as properties:
for e.g
Property Property Value
firstName John
lastName Doe
age 50
eyeColor blue
Accessing object properties , basically you can access properties in two
ways
1) objectName.propertyName ()
2) objectName['propertyName']
*/
// console.log(person.firstName);
// console.log(person['lastName']);
// Object Methods
/*
Objects can also have methods
Methods are basically actions performed on methods
Methods are stored in properties as function definitions
Property Property Value
firstName John
lastName Doe
age 50
eyeColor blue
fullName function(){
return this.firstName + + this.lastname
};
Basically A Method is a function which is stored as property
*/
// var person = {
// firstName:'chinmay',
// lastName: 'deshpande',
// age:89,
// eyeColor:'blue',
// fullName: function(){
// return `${this.firstName} ${this.lastName}`
// // Below is the old way
// //return " this.firstName +' '+this.lastName"
// }
//
// };
//
// console.log(person.fullName());
// console.log(person.fullName);
/*
The this keyword
1) In a function definition , this refer to the "owner" of the function.
2) In the example obove , this is the person object that "owns" the fullName functions
3) In the other words , this .firstName means the first property of this object.
// Accessing object methods
// you can access object method with following methods
objectName.methodName()
4) if you access the function without parameters , it will return function
definition
5) When a JavaScript variable is declared with the keyword "new",
the variable is created as an object.
*/