Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab03 zadaca 2
- function Singleton() {
- if (Singleton.instance) {
- return Singleton.instance;
- }
- Singleton.instance = this;
- this.dateCreated = Date.now();
- }
- console.log('Testing Singleton!');
- console.log('Creating i1');
- let i1 = new Singleton();
- console.log('i1 was created at time[Number ' + i1.dateCreated + ']');
- console.log('Creating i2');
- let i2 = new Singleton();
- console.log('i2 was created at time[Number ' + i2.dateCreated + ']');
- console.log('Checking if i1 and i2 are the same variable: ', i1 === i2);
- console.log('Creating i3 in different execution context at time: ', Date.now());
- function newContext() {
- let i3 = new Singleton();
- console.log('i3 was created at time[Number ' + i3.dateCreated + ']');
- return i3;
- }
- newContext()
- console.log('Checking if i1 and i3 are the same variable: ', i1 === newContext());
- console.log('Checking if i2 and i3 are the same variable: ', i2 === newContext());
- console.log('Checking if i1 and i2 are the same variable: ', i1 === i2);
- //Lab03 zadaca 3
- Array.prototype.fillDefault = function(param){
- for(let i = 0; i<this.length; i++){
- if(this[i] == undefined){
- this[i]=param
- }
- }
- return this
- }
- let x = [1,2,3]
- x[10] = 100
- x[8] = 8
- console.log(x.toString())
- x.fillDefault(0)
- console.log(x.toString())
- //Deep clone
- function deepCopy(obj) {
- if(obj == null || typeof(obj) !== 'object'){
- return obj;
- }
- var deep = Object.create(obj.constructor.prototype); // or just: var deep = {};
- for(var key in obj){
- deep[key] = deepCopy(obj[key]);
- }
- return deep;
- }
- const original = {
- name: 'Fiesta',
- car: {
- color: 'blue'
- },
- sinka(){
- return "DONPLEJA SINE"
- }
- }
- var copied = deepCopy(original)
- original.name = "smeneto ime bratu..."
- original.car.color = "red"
- original.sinka = function smeneta(){
- return "BABA TIII"
- }
- console.log(original)
- console.log(copied)
- //Ispit 2018 prva zadacata
- function call_up_to(funkcija,x) {
- debugger
- let rez
- let counter=0
- let finalRez
- return function(){
- if(funkcija){
- rez = funkcija.apply(this)
- counter++
- finalRez = rez
- }
- if(counter>=x){
- return funkcija = rez
- }
- return rez
- }
- }
- let dame = call_up_to(function(){
- debugger
- console.log(Math.random(1,3))
- },3)
- dame()
- dame()
- dame()
- dame()
- dame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement