An Warware: Samun boolean bazuwar

Babban matsalar Get bazuwar boolean shine rashin tsaro. Duk wanda ke da damar shiga uwar garken zai iya samar da ฦ™imar boolean na sabani.

var randomBoolean = Math.random() >= 0.5;

Wannan layin lambar yana ba da ฦ™imar boolean bazuwar zuwa madaidaicin โ€œrandomBooleanโ€. ฦ˜imar boolean ko dai gaskiya ne ko ฦ™arya, kuma aikin Math.random() ne ya samar dashi. Wannan aikin yana dawo da lambar bazuwar tsakanin 0 da 1, kuma idan wannan lambar ta fi ko daidai da 0.5, to ฦ™imar boolean zata zama gaskiya. In ba haka ba, zai zama ฦ™arya.

Menene aji

?

Aji a JavaScript samfuri ne don ฦ™irฦ™irar abubuwa.

Fadada Darasi

ฦ˜arfafa azuzuwan sifa ce ta JavaScript wanda ke ba ku damar ฦ™irฦ™irar azuzuwan al'ada waษ—anda za a iya amfani da su a cikin lambar ku. Wannan na iya zama da amfani idan kuna buฦ™atar ฦ™irฦ™irar nau'in al'ada ko kuma idan kuna buฦ™atar ฦ™ara takamaiman ayyuka zuwa nau'in da ke akwai.

Don ฦ™irฦ™irar ฦ™arin aji a JavaScript, da farko kuna buฦ™atar ฦ™irฦ™irar sabon fayil mai suna extend.js kuma ฦ™ara lambar mai zuwa:

var MyClass = aiki (){}; MyClass.prototype = {}; MyClass.extend = aiki (sauran){wannan.prototype = other.prototype; };

Na gaba, kuna buฦ™atar ayyana kaddarorin da hanyoyin sabon ajin ku. Kuna yin haka ta ฦ™ara kaddarori da hanyoyi zuwa abin samfurin MyClass. Ga misali:

MyClass.prototype._myMethod = aiki(){}; MyClass.prototype._anotherMethod = aiki(){}; MyClass.extend ({_myMethod: aiki(){}, _anotherMethod: aiki(){}});

A ฦ™arshe, kuna buฦ™atar yin rajistar sabon ajin ku tare da mai tara JavaScript ta ฦ™ara layin da ke gaba zuwa lambar ku:

MyClass._register = aiki(){};

Shafi posts:

Leave a Comment