Advertisement
Guest User

za kiki

a guest
Nov 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. //Lab03 zadaca 2
  2. function Singleton() {
  3. if (Singleton.instance) {
  4. return Singleton.instance;
  5. }
  6. Singleton.instance = this;
  7. this.dateCreated = Date.now();
  8. }
  9.  
  10. console.log('Testing Singleton!');
  11. console.log('Creating i1');
  12. let i1 = new Singleton();
  13. console.log('i1 was created at time[Number ' + i1.dateCreated + ']');
  14. console.log('Creating i2');
  15. let i2 = new Singleton();
  16. console.log('i2 was created at time[Number ' + i2.dateCreated + ']');
  17. console.log('Checking if i1 and i2 are the same variable: ', i1 === i2);
  18. console.log('Creating i3 in different execution context at time: ', Date.now());
  19.  
  20. function newContext() {
  21. let i3 = new Singleton();
  22. console.log('i3 was created at time[Number ' + i3.dateCreated + ']');
  23.  
  24. return i3;
  25. }
  26.  
  27. newContext()
  28. console.log('Checking if i1 and i3 are the same variable: ', i1 === newContext());
  29. console.log('Checking if i2 and i3 are the same variable: ', i2 === newContext());
  30. console.log('Checking if i1 and i2 are the same variable: ', i1 === i2);
  31.  
  32. //Lab03 zadaca 3
  33.  
  34.  
  35. Array.prototype.fillDefault = function(param){
  36. for(let i = 0; i<this.length; i++){
  37. if(this[i] == undefined){
  38. this[i]=param
  39.  
  40. }
  41. }
  42. return this
  43. }
  44.  
  45. let x = [1,2,3]
  46. x[10] = 100
  47. x[8] = 8
  48. console.log(x.toString())
  49. x.fillDefault(0)
  50. console.log(x.toString())
  51. //Deep clone
  52. function deepCopy(obj) {
  53. if(obj == null || typeof(obj) !== 'object'){
  54. return obj;
  55. }
  56.  
  57. var deep = Object.create(obj.constructor.prototype); // or just: var deep = {};
  58.  
  59. for(var key in obj){
  60. deep[key] = deepCopy(obj[key]);
  61. }
  62. return deep;
  63. }
  64.  
  65. const original = {
  66. name: 'Fiesta',
  67. car: {
  68. color: 'blue'
  69. },
  70. sinka(){
  71. return "DONPLEJA SINE"
  72. }
  73.  
  74. }
  75.  
  76. var copied = deepCopy(original)
  77. original.name = "smeneto ime bratu..."
  78. original.car.color = "red"
  79. original.sinka = function smeneta(){
  80. return "BABA TIII"
  81. }
  82. console.log(original)
  83. console.log(copied)
  84.  
  85. //Ispit 2018 prva zadacata
  86. function call_up_to(funkcija,x) {
  87. debugger
  88. let rez
  89. let counter=0
  90. let finalRez
  91. return function(){
  92. if(funkcija){
  93. rez = funkcija.apply(this)
  94. counter++
  95. finalRez = rez
  96. }
  97. if(counter>=x){
  98. return funkcija = rez
  99. }
  100. return rez
  101.  
  102. }
  103. }
  104. let dame = call_up_to(function(){
  105. debugger
  106. console.log(Math.random(1,3))
  107. },3)
  108.  
  109. dame()
  110. dame()
  111. dame()
  112. dame()
  113. dame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement