SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Firefox 4   JavaScript / Narcissus Асен Божилов   http://asenbozhilov.com
 
Object API
Object API Object. defineProperty Object. getOwnPropertyDescriptor Object. defineProperties Object. getPrototypeOf Object. create Object. keys Object. getOwnPropertyNames Object. freeze Object. isFrozen Object. preventExtensions Object. isExtensible Object. seal Object. isSealed
var  foo = { bar  :  true }; for  ( var  i  in  foo) { console. log (i);  //bar  } foo.bar =  false ;  //Присвояване на нова стойност console. log (foo.bar);  //false console. log ( delete  foo.bar);  //true
Object. defineProperty  (obj, prop, desc)
var  foo = {}; Object. defineProperty (foo,  'bar' , { value  :  'baz' , enumerable  :  true , writable  :  true , configurable  :  false });
// [[Value]]  var  foo = {}; Object. defineProperty (foo,  'bar' , { value  :  'baz' , enumerable  :  true , writable  :  true , configurable  :  false }); console. log (foo.bar);  //baz
// [[ E numerable ]]  var  foo = {}; Object. defineProperty (foo,  'bar' , { value  :  'baz' , enumerable  : true , writable  :  true , configurable  :  false }); for  ( var  i  in  foo) { console. log (i);  //bar }
// [[Writable]]  var  foo = {}; Object. defineProperty (foo,  'bar' , { value  :  'baz' , enumerable  :  true , writable  : true , configurable  :  false }); foo.bar =  'new value' ; console. log (foo.bar);  //new value
// [[Configurable]]  var  foo = {}; Object. defineProperty (foo,  'bar' , { value  :  'baz' , enumerable  :  true , writable  :  true , configurable  : false }); console. log ( delete  foo.bar);  //false console. log (foo.bar);  //new value
// Setter and Getter var  foo = {}; ( function  () { var  _private; Object. defineProperty (foo,  'bar' , { set  :  function  (val) { _private  = val; }, get  :  function  () { return  _private; } }); })(); foo.bar =  10 ;  //Извикване на setter-а  foo.bar;  //Извикване на getter-а
// Стойности по подразбиране var  foo = {}; Object. defineProperty (foo,  'bar' , { value  :  'baz' });
/ / Стойности по подразбиране
// Стойности по подразбиране var  foo = {}; Object. defineProperty (foo,  'bar' , { value  :  'baz' }); for  ( var  i  in  foo) { console. log (i);  //bar няма да бъде изброено от for-in } foo.bar =  'new value' ; console. log (foo.bar);  //baz console. log ( delete  foo.bar);  //false
Object. getOwnPropertyDescriptor  (obj, prop)
//Object.getOwnPropertyDescriptor var  foo = {bar :  true }, desc  = Object. getOwnPropertyDescriptor (foo,  'bar' ); console. dir (desc); /* value  : true enumerable  : true configurable : true writable  : true */
Object. defineProperties  (obj, properties)
//Object.defineProperties var  foo = {}; Object. defineProperties (foo, { bar  : { value  :  true , enumerable  :  true , writable  :  false , configurable  :  false }, baz  : { value  :  false } });
/ /Object.defineProperties var  foo = {}, properties  = { bar  : { value  :  true , enumerable  :  true , writable  :  false , configurable  :  false }, baz  : { value  :  false } }; //Емулация на Object.defineProperties var  hasOwnProp = Object. prototype .hasOwnProperty; for  ( var  i  in  properties) { if  (hasOwnProp. call (properties, i)) { Object. defineProperty (foo, i, properties[i]); } }
//Наследяване и използване на [[Prototype]] var  foo = {}; console. log ( typeof  foo.hasOwnProperty);  //function console. log (foo. hasOwnProperty ( 'hasOwnProperty' ));  //false console. log (Object. prototype . hasOwnProperty ( 'hasOwnProperty' ));  //true
//Наследяване и използване на [[Prototype]] var  foo = { [[Prototype]] : Object. prototype };
Object. getPrototypeOf  (obj)
//Object.getPrototypeOf var  foo = {}; Object. getPrototypeOf (foo) === Object. prototype ;  //true
/ /Итериране през прототипната верига на обект function   getValueOf (obj, prop) { var  hasOwnProp = Object. prototype .hasOwnProperty; do  { if  (hasOwnProp. call (obj, prop)) { return  obj[prop]; } }  while  ( obj = Object. getPrototypeOf (obj) ); //Ако свойство с подаденото име не съществува в прототипната верига се връща стойност undefined return  undefined; } var  foo = {bar :  10 }; console. log (foo.bar);  //10 console. log ( getValueOf (foo,  'bar' ));  //10 console. log (foo.foo);  //undefined console. log ( getValueOf (foo,  'foo' ));  //undefined
Object. create  (proto [, properties ])
//Object.create var  foo = {a :  10 , b :  20 }; var  bar = Object. create (foo); console. log (bar.a);  //10 console. log (bar.b);  //20 Object. getPrototypeOf (bar) === foo;  //true
//Object.create /* * Създава се обект, който е на върха на прототипната си верига. * За стойност на [[Prototype]] има null и не наследява свойства. */ var  map = Object. create ( null );
Object. keys  (obj)
//Object.keys var  obj = { foo  :  1 , bar  :  2 }; /* * Дефиниране на свойство с [[Enumerable]] атрибут false */ Object. defineProperty (obj,  'baz' , {value :  3 }); /* * Взимане на масив с имената на всички изброими собствени свойства * Object.keys не попълва масива с наследени свойства */ Object. keys (obj);  //["foo", "bar"]
Object. getOwnPropertyNames  (obj)
//Object.getOwnPropertyNames var  obj = { foo  :  1 , bar  :  2 }; /* * Дефиниране на свойство което има [[Enumerable]] атрибут false */ Object. defineProperty (obj,  'baz' , {value :  3 }); /* * Взимане на масив с имената на всички собствени свойства, независимо дали са изброими или не * Object.getOwnPropertyNames не попълва масива с наследени свойства */ Object. getOwnPropertyNames (obj);  //["foo", "bar", "baz"]
Object. preventExtensions  (obj)  /  Object. isExtensible  (obj)
//Object.preventExtensions / Object.isExtensible var  foo = { bar  :  1 ,  baz  :  2 }; /*   * Object.preventExtensions *  - спира добавянето на нови свойства в обекта */   Object. preventExtensions (foo); foo.newProperty =  3 ; console. log (foo.newProperty);  //undefined
//Object.preventExtensions / Object.isExtensible var  foo = {}, bar  = {}; Object. preventExtensions (foo); console. log (Object. isExtensible (foo));  //false console. log (Object. isExtensible (bar));  //true
Object. seal  (obj)  /  Object. isSealed  (obj)
// Object.seal / Object.isSealed var  foo = { bar  :  1 ,  baz  :  2 }; /*  *  - спира премахването на текущите свойства от обекта *  - не могат да бъдат добавяни нови свойства на обекта */   Object. seal (foo); console. log ( delete  foo.bar);  //false foo.newProperty =  3 ; console. log (foo.newProperty);  //undefined /* * Текущите стойности на свойствата могат да бъдат променени */ foo.bar =  6 ; console. log (foo.bar);  //6
// Object.seal / Object.isSealed var  foo = {}, bar  = {}; Object. seal (foo); console. log (Object. isSealed (foo));  //true console. log (Object. isSealed (bar));  //false
Object. freeze  (obj)  /  Object. isFrozen  (obj)
// Object.freeze / Object.isFrozen var  foo = { bar  :  1 ,  baz  :  2 }; /* *  - предотвратява премахването на текущите свойства *  - не могат да бъдат добавяни нови свойства *  - текущите стойности на свойствата  НЕ  могат да бъдат променяни */   Object. freeze (foo); console. log ( delete  foo.bar);  //false foo.newProperty =  3 ; console. log (foo.newProperty);  //undefined foo.bar =  6 ; console. log (foo.bar);  //1
// Object.freeze / Object.isFrozen var  foo = {}, bar  = {}; Object. freeze (foo); console. log (Object. isFrozen (foo));  //true console. log (Object. isFrozen (bar));  //false
Narcissus
Narcissus ,[object Object]
Написан на JavaScript
Поддръжка на бъдещи разширения на JavaScript
Интеграция в Firefox 4 чрез разширението  Zaphod

Weitere ähnliche Inhalte

Empfohlen

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

OpenCamp Sofia

  • 1. Firefox 4 JavaScript / Narcissus Асен Божилов http://asenbozhilov.com
  • 2.  
  • 4. Object API Object. defineProperty Object. getOwnPropertyDescriptor Object. defineProperties Object. getPrototypeOf Object. create Object. keys Object. getOwnPropertyNames Object. freeze Object. isFrozen Object. preventExtensions Object. isExtensible Object. seal Object. isSealed
  • 5. var foo = { bar : true }; for ( var i in foo) { console. log (i); //bar } foo.bar = false ; //Присвояване на нова стойност console. log (foo.bar); //false console. log ( delete foo.bar); //true
  • 6. Object. defineProperty (obj, prop, desc)
  • 7. var foo = {}; Object. defineProperty (foo, 'bar' , { value : 'baz' , enumerable : true , writable : true , configurable : false });
  • 8. // [[Value]] var foo = {}; Object. defineProperty (foo, 'bar' , { value : 'baz' , enumerable : true , writable : true , configurable : false }); console. log (foo.bar); //baz
  • 9. // [[ E numerable ]] var foo = {}; Object. defineProperty (foo, 'bar' , { value : 'baz' , enumerable : true , writable : true , configurable : false }); for ( var i in foo) { console. log (i); //bar }
  • 10. // [[Writable]] var foo = {}; Object. defineProperty (foo, 'bar' , { value : 'baz' , enumerable : true , writable : true , configurable : false }); foo.bar = 'new value' ; console. log (foo.bar); //new value
  • 11. // [[Configurable]] var foo = {}; Object. defineProperty (foo, 'bar' , { value : 'baz' , enumerable : true , writable : true , configurable : false }); console. log ( delete foo.bar); //false console. log (foo.bar); //new value
  • 12. // Setter and Getter var foo = {}; ( function () { var _private; Object. defineProperty (foo, 'bar' , { set : function (val) { _private = val; }, get : function () { return _private; } }); })(); foo.bar = 10 ; //Извикване на setter-а foo.bar; //Извикване на getter-а
  • 13. // Стойности по подразбиране var foo = {}; Object. defineProperty (foo, 'bar' , { value : 'baz' });
  • 14. / / Стойности по подразбиране
  • 15. // Стойности по подразбиране var foo = {}; Object. defineProperty (foo, 'bar' , { value : 'baz' }); for ( var i in foo) { console. log (i); //bar няма да бъде изброено от for-in } foo.bar = 'new value' ; console. log (foo.bar); //baz console. log ( delete foo.bar); //false
  • 17. //Object.getOwnPropertyDescriptor var foo = {bar : true }, desc = Object. getOwnPropertyDescriptor (foo, 'bar' ); console. dir (desc); /* value : true enumerable : true configurable : true writable : true */
  • 18. Object. defineProperties (obj, properties)
  • 19. //Object.defineProperties var foo = {}; Object. defineProperties (foo, { bar : { value : true , enumerable : true , writable : false , configurable : false }, baz : { value : false } });
  • 20. / /Object.defineProperties var foo = {}, properties = { bar : { value : true , enumerable : true , writable : false , configurable : false }, baz : { value : false } }; //Емулация на Object.defineProperties var hasOwnProp = Object. prototype .hasOwnProperty; for ( var i in properties) { if (hasOwnProp. call (properties, i)) { Object. defineProperty (foo, i, properties[i]); } }
  • 21. //Наследяване и използване на [[Prototype]] var foo = {}; console. log ( typeof foo.hasOwnProperty); //function console. log (foo. hasOwnProperty ( 'hasOwnProperty' )); //false console. log (Object. prototype . hasOwnProperty ( 'hasOwnProperty' )); //true
  • 22. //Наследяване и използване на [[Prototype]] var foo = { [[Prototype]] : Object. prototype };
  • 24. //Object.getPrototypeOf var foo = {}; Object. getPrototypeOf (foo) === Object. prototype ; //true
  • 25. / /Итериране през прототипната верига на обект function getValueOf (obj, prop) { var hasOwnProp = Object. prototype .hasOwnProperty; do { if (hasOwnProp. call (obj, prop)) { return obj[prop]; } } while ( obj = Object. getPrototypeOf (obj) ); //Ако свойство с подаденото име не съществува в прототипната верига се връща стойност undefined return undefined; } var foo = {bar : 10 }; console. log (foo.bar); //10 console. log ( getValueOf (foo, 'bar' )); //10 console. log (foo.foo); //undefined console. log ( getValueOf (foo, 'foo' )); //undefined
  • 26. Object. create (proto [, properties ])
  • 27. //Object.create var foo = {a : 10 , b : 20 }; var bar = Object. create (foo); console. log (bar.a); //10 console. log (bar.b); //20 Object. getPrototypeOf (bar) === foo; //true
  • 28. //Object.create /* * Създава се обект, който е на върха на прототипната си верига. * За стойност на [[Prototype]] има null и не наследява свойства. */ var map = Object. create ( null );
  • 29. Object. keys (obj)
  • 30. //Object.keys var obj = { foo : 1 , bar : 2 }; /* * Дефиниране на свойство с [[Enumerable]] атрибут false */ Object. defineProperty (obj, 'baz' , {value : 3 }); /* * Взимане на масив с имената на всички изброими собствени свойства * Object.keys не попълва масива с наследени свойства */ Object. keys (obj); //["foo", "bar"]
  • 32. //Object.getOwnPropertyNames var obj = { foo : 1 , bar : 2 }; /* * Дефиниране на свойство което има [[Enumerable]] атрибут false */ Object. defineProperty (obj, 'baz' , {value : 3 }); /* * Взимане на масив с имената на всички собствени свойства, независимо дали са изброими или не * Object.getOwnPropertyNames не попълва масива с наследени свойства */ Object. getOwnPropertyNames (obj); //["foo", "bar", "baz"]
  • 33. Object. preventExtensions (obj) / Object. isExtensible (obj)
  • 34. //Object.preventExtensions / Object.isExtensible var foo = { bar : 1 , baz : 2 }; /* * Object.preventExtensions * - спира добавянето на нови свойства в обекта */ Object. preventExtensions (foo); foo.newProperty = 3 ; console. log (foo.newProperty); //undefined
  • 35. //Object.preventExtensions / Object.isExtensible var foo = {}, bar = {}; Object. preventExtensions (foo); console. log (Object. isExtensible (foo)); //false console. log (Object. isExtensible (bar)); //true
  • 36. Object. seal (obj) / Object. isSealed (obj)
  • 37. // Object.seal / Object.isSealed var foo = { bar : 1 , baz : 2 }; /* * - спира премахването на текущите свойства от обекта * - не могат да бъдат добавяни нови свойства на обекта */ Object. seal (foo); console. log ( delete foo.bar); //false foo.newProperty = 3 ; console. log (foo.newProperty); //undefined /* * Текущите стойности на свойствата могат да бъдат променени */ foo.bar = 6 ; console. log (foo.bar); //6
  • 38. // Object.seal / Object.isSealed var foo = {}, bar = {}; Object. seal (foo); console. log (Object. isSealed (foo)); //true console. log (Object. isSealed (bar)); //false
  • 39. Object. freeze (obj) / Object. isFrozen (obj)
  • 40. // Object.freeze / Object.isFrozen var foo = { bar : 1 , baz : 2 }; /* * - предотвратява премахването на текущите свойства * - не могат да бъдат добавяни нови свойства * - текущите стойности на свойствата НЕ могат да бъдат променяни */ Object. freeze (foo); console. log ( delete foo.bar); //false foo.newProperty = 3 ; console. log (foo.newProperty); //undefined foo.bar = 6 ; console. log (foo.bar); //1
  • 41. // Object.freeze / Object.isFrozen var foo = {}, bar = {}; Object. freeze (foo); console. log (Object. isFrozen (foo)); //true console. log (Object. isFrozen (bar)); //false
  • 43.
  • 45. Поддръжка на бъдещи разширения на JavaScript
  • 46. Интеграция в Firefox 4 чрез разширението Zaphod
  • 47. Изпълнение на единичен скрипт през Narcissus <script type = &quot;application/narcissus&quot; > //... </script> <script type = &quot;text/narcissus&quot; > //... </script> Изполване на Narcissus?
  • 48. Изпълнение на всички скриптове в страницата през Narcissus. (независимо от type атрибута им) <meta http-equiv = &quot;Content-Script-Type&quot; content = &quot;application/narcissus&quot; > <meta http-equiv = &quot;Content-Script-Type&quot; content = &quot;text/narcissus&quot; > Изполване на Narcissus?
  • 49. Конец Автор: Асен Божилов Благодарности на: Dmitry Soshnikov