Memaparkan catatan dengan label JavaScript. Papar semua catatan
Memaparkan catatan dengan label JavaScript. Papar semua catatan

Javascript : Object 1

Objek membenarkan kita menyimpan properties dan beberapa nilai bagi object itu sendiri. Bagaimana kita nak membuat simple Object? Declare object perlu ada: 1) var 2) diikuti dengan nama objek 3) diikuti dengan tanda sama = 4) Setiap objek kemudian mempunyai tanda { 5) ada info didalam breket { tadi 6) berakhir dengan } Ini adalah contoh dimana info tiada didalam breket.

var bob = {};


Ini pula contoh objek terdiri daripada info yang dikenali sebagai property. Kaedah memasukkan property adalah nama property, diikuti tanda noktah bertindih( : ) dan nilai bagi property tersebut. Jika kita mempunyai banyak property, ianya perlu dipisahkan dengan tanda koma ( , ). Contoh seperti dibawah:



var Spencer = {
age: 22,
country: "United States"
};

// make your own object here called me
var me = {
age:27,
country:"Pulai Chondong"
};


Akses Properties Dalam Object Cara Pertama

Guna Object Literal Notation - Dot Notation

Object literal notation adalah mencipta objek baru dan meletakkan propeties didalam braket {}. Object literal notation mempunyai dua iaitu Dot Notation ( . ) dan Bracket Notation {} .

1) Dot Notation

Cara mudah adalah Var = .



var bob = {
name: "Bob Smith",
age: 30
};
var susan = {
name: "Susan Jordan",
age: 25
};
// here we save Bob's information
var name1 = bob.name;
var age1 = bob.age;
// finish this code by saving Susan's information
var name2 =susan.name;
var age2 = susan.age;


Akses Properties Dalam Object Cara Kedua

Guna Object literal notation - Bracket Notation


Object literal notation adalah mencipta objek baru dan meletakkan propeties didalam curly bracket {}. Object literal notation mempunyai dua iaitu Dot Notation ( . ) dan Bracket Notation [] .

1) Bracket Notation

// Take a look at our next example object, a dog
var dog = {
species: "greyhound",
weight: 60,
age: 4
};

var species = dog["species"];
// fill in the code to save the weight and age using bracket notation
var weight = dog["weight"];
var age =dog["age"];


Akses Properties Dalam Object Cara Ketiga
Constructor

Apa itu constructor? Constructor adalah cara mencipta objek tanpa curly bracket {}. Cipta objek menggunakan constructor iaitu mencipta objek kosong terlebih dahulu () dan syntaxnya adalah :


var objectName = new Object();


Kita sudah istihar objek tanpa properties.



var bob = new Object();



Bagaimana pula ingin masukkan properties? dengan cara kita create nama property tersebut contoh nama @ umur selepas . dan kemudian assign kepada nilai yang dikehendaki. Lihat contoh:


var bob = new Object();
bob.nama = "Bob Amirul";
bob.umur = 30;

Javascript: For (var property in dog)


var dog = {
species: "bulldog",
age: 3,
color: brown
};

for(var property in dog) {
console.log(property);
}


The words var and in are keywords. We need them to always be there. We can replace dog with any object that we want the for-in loop to run through. And you can think of property as a placeholder variable. You can use any word you want here.

In English, what is the code doing? It says: Assign the first property of the dog object to the variable property. Run the code (here, it is to print property to console). Then assign the second property of the dog object to the variable property. Again, run the code in the curly brackets. Keep repeating this until all the properties of the dog have been assigned to property.

Javascript: Object

 

- - - B a s i c s - - -

Each Object has one or more properties.
Each property consists of a property-key and it's associated value.

 var object1 = {
name: "First"
}


So object1 has 1 property
a name property with property-key name and it's associated string VALUE "FIRST"

OR

var myObj = {
type: 'fancy',
disposition: 'sunny'
}


myObj has 2 properties seperated by a comma-,,
a type property with property-key type and an associated string VALUE 'fancy'
a disposition-property with property-key disposition and
..an associated string VALUE 'sunny'.

= = = = = = = = = = = = = = = = = = = = = = =
To create an Object,
you can use the literal notation,
you directly create an Instance of the object, with the
properties being separated by a comma-,


var myObj = {
type: 'fancy',
disposition: 'sunny'
};


OR
You create an Object by the construct notation.
First you create an empty Object by way of either
myObj = new Object(); or myObj = {};
and then you attach its properties using the syntax
object-name.property-key = it's-associated-value ;
( this.name = x ; )

thus:

var myObj = {};
myObj.type = 'fancy';
myObj.disposition = 'sunny';



OR

There is also the facility Class construct notation.
The name should then start with a Capital-letter



var Person = function( theName, theAge ) {
this.name = theName;
this.age = theAge;
this.displayInstance = function() {
console.log("The displayInstance -output-"+
"\n============================" +
"\n\t name: " + this.name +
"\n\t age: " + this.age);
};
};
//now create an Instance of this Class-object
var myObj = new Person("Classy_Rocker",20);
//call the Method displayInstance which takes NO parameters
myObj.displayInstance();
console.log( myObj );


As you can see i created a function within this constructor,
they now call this function a Method.
So if in near future the course is asking you to create a method you now know
that you have to create
a property-key with an associated value being a function within an Object.

Javascript: Loop Mastery Practice

 

LatihanKetiga2Loop

LATIHAN

Laksanakan ketiga-tiga For, Do/While dan While dalam satu aturcara.

// Write your code below!

var testFor= function(){
for (i=1;i<10;i++){
console.log ("ini adalah loop bagi for" +i);
};
};


var testDoWhile= function(){
hidupkanLoopDo=true;
do {
console.log ("Ini adalah loop bagi do untuk kali ini sahaja");
}while (hidupkanLoopDo=false);

};

var testWhile=function(){
i=1;
while(i<10){
console.log ("Ini adalah loop bagi While yang ke"+i);
++i;
}
}

testFor();
testDoWhile();
testWhile();







ini adalah loop bagi for1
ini adalah loop bagi for2
ini adalah loop bagi for3
ini adalah loop bagi for4
ini adalah loop bagi for5
ini adalah loop bagi for6
ini adalah loop bagi for7
ini adalah loop bagi for8
ini adalah loop bagi for9
Ini adalah loop bagi do untuk kali ini sahaja
Ini adalah loop bagi While yang ke1
Ini adalah loop bagi While yang ke2
Ini adalah loop bagi While yang ke3
Ini adalah loop bagi While yang ke4
Ini adalah loop bagi While yang ke5
Ini adalah loop bagi While yang ke6
Ini adalah loop bagi While yang ke7
Ini adalah loop bagi While yang ke8
Ini adalah loop bagi While yang ke9


Javascript : Do / While

 

Kadang-kadang, kita ingin loop kita berjalan at least satu kali. Oleh itu kita akan menggunakan Do/While untuk jalan mudah.

var loopCondition = false;

do {
console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");
} while (loopCondition);


It runs once because do tells it to, but then never again because loopCondition is false!



LATIHAN



1) Guna Do/While.

2) Minta input dari user


3) Display di console.log


4) Matikan dengan falsekan kenyataan.



var getToDaChoppa = function(){
// Write your do/while loop here!
do {
var x = prompt ("Apa pilihan anda?");
console.log (x);
} while (jalankan=false);

};

getToDaChoppa();

Javascript : While Part 1

 

Jika for digunakan ketika kita tahu bila ia berhenti, While pula digunakan ketika kita tidak tahu bilakah dia akan berhenti.

 

understand = true;

while(understand === true){
console.log("I'm learning while loops!");
//Change the value of 'understand' here!
understand=false;

}


Namun pastikan didalam while tersebut, ada jalan untuk dia berhenti dan keluar dari while tersebut dan tidaklah ianya berpusing sahaja forever dan akhirnya membawa kepada crash sesuatu sistem akibat while yang tidak keluar2 atau infinity.


contoh diatas adalah understand=false;  adalah satu cara untuk kita hentikan penyataan while.


Brevity is the soul of programming


Ketahuilah brevity adalah key penting dlm programming, oleh itu, code diatas kita boleh ringkaskan menjadi:

understand = true;

while(understand){
console.log("I'm learning while loops!");
//Change the value of 'understand' here!
understand=false;

}

understand itu kita tidak perlu tanya ===true didalam while.


Penyataan diringkaskan seperti dibawah:

var bool = true;
while(bool){
//Do something
}


daripada:

var bool = true;
while(bool === true){
//Do something
}

 


Kedua-dua adalah sama sahaja, melainkan brevity dan kecepatan meringkas sahaja. Elakkan written the less succinct version dalam programming kita, sebaliknya teruskan  Correct it to the more elegant version!

var bool = true;

while(bool){
console.log("Less in coding is too cool!");
bool = false;
}
LATIHAN
Bina satu coding menggunakan while, dengan penyataaan I'm looping! sebanyak tiga kali menggunakan while.
Bagaimana menggunakan counter untuk while? Set variable kepada 0 di luar While, dan set counter tersebut bertambah semasa looping, dan set penyataan berhenti di While dengan nyatakan bilangan ke berapa ianya patut berhenti atau berapa kali ianya TRUE.
//Remember to set your condition outside the loop!


var loop = function(){
while(count <3){
//Your code goes here!
console.log("I'm looping!");
count++;
}
};

var count=0;
loop();





I'm looping!
I'm looping!
I'm looping!

 


CONTOH

//Remember to make your condition true outside the loop!

var soloLoop = function(){
//Your code goes here!

loopSekali=true;

while (loopSekali){
console.log("Looped once!");
loopSekali = false;
}

};

soloLoop();





Looped once!