Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Coding JavaScript the expressive way Learning & Development Telerik Software Academy.
Asynchronous Programming with C# and WinRT
Touch and Gestures with Xamarin Forms
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
With Mocha and Karma Telerik Academy Telerik Software Academy.
C# Fundamentals – Part I
Welcome to the JSON-stores world Telerik Software Academy Databases.
NoSQL Concepts, Redis, MongoDB, CouchDB
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Svetlin Nakov Technical Trainer Software University Functions and Objects Reusable Parts of Code Objects in JavaScript.
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Telerik Software Academy End-to-end JavaScript Applications.
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
Closures, Function Scope, Nested Functions Learning & Development Team Telerik Software Academy.
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
The past, the present, the future Telerik School Academy HTML, CSS and JavaScript.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Course Overview Doncho Minkov Telerik Software Academy Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Using the Standard.NET Framework Classes Telerik Software Academy C# Fundamentals – Part 2.
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Associative Arrays and Objects Associative Arrays, Objects Svetlin Nakov Technical Trainer Software University
Telerik Software Academy Databases.
Svetlin Nakov Technical Trainer Software University Functions and Objects Reusable Parts of Code Objects in JavaScript.
Integer, Floating-Point, Text Data, Variables, Literals Telerik Corporation
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Definition, Constructors, Methods, Access Modifiers, Static/Instance members, Learning & Development Team Telerik Software Academy.
Presentation transcript:

Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy

 Classes and Objects  JavaScript Objects Overview  Reference and Primitive Types  JSON Objects  JavaScript Object Properties  Associative Arrays

Modeling Real-world Entities with Objects

 Software objects model real-world objects or abstract concepts  Examples:  bank, account, customer, dog, bicycle, queue  Real-world objects have states and behaviors  Account' states:  holder, balance, type  Account' behaviors:  withdraw, deposit, suspend

 How do software objects implement real- world objects?  Use variables/data to implement states  Use methods/functions to implement behaviors  An object is a software bundle of variables and related methods

6  checks  people  shopping list …  numbers  characters  queues  arrays Things from the real world Things from the computer world

 The formal definition of class: Definition by Google Classes act as templates from which an instance of an object is created at run time. Classes define the properties of the object and the methods used to control the object's behavior.

 Classes provide the structure for objects  Define their prototype, act as template  Classes define:  Set of attributes  Represented by variables and properties  Hold their state  Set of actions (behavior)  Represented by methods  A class defines the methods and types of data associated with an object

Account +Owner: Person +Ammount: double +Suspend()+Deposit(sum:double)+Withdraw(sum:double) Class Name Attributes (Properties and Fields) Operations(Methods)

 An object is a concrete instance of a particular class  Creating an object from a class is called instantiation  Objects have state  Set of values associated to their attributes  Example:  Class: Account  Objects: Ivan's account, Peter's account

Account +Owner: Person +Ammount: double +Suspend()+Deposit(sum:double)+Withdraw(sum:double) Class ivanAccount +Owner="Ivan Kolev" +Ammount= peterAccount +Owner="Peter Kirov" +Ammount= kirilAccount +Owner="Kiril Kirov" +Ammount=25.0 Object Object Object

What are Objects?

 JavaScript is designed on a simple object- based paradigm  An object is a collection of properties  An object property is association with a name and a value  A value of property can be either a method (function) or a field (variable)  Lots of predefined objects available in JS  Math, document, window, etc…  Objects can be created by the developer

 Each object has properties  Properties are variables attached to the object  Properties of an object can be accessed with a dot-notation: var arrStr = arr.join(", "); // property join of Array var length = arr.length; // property length of Array var words = text.split(" ");

Live Demo

The Types in JavaScript

 JavaScript is a dynamic language  Variables don’t have type, but their values do  JavaScript has six different types:  Number, String, Boolean, Null, Undefined and Object  Object is the only reference type  Copied by reference  Number, String, Boolean, Null, Undefined are primitive types  Copied by value

 The primitive types are Boolean, Number, String, Undefined and Null  All the other types are actually of type object  Including arrays, dates, custom types, etc… console.log(typeof new Object() === typeof new Array()); // true console.log(typeof new Object() === typeof new Date()); // true console.log(typeof new Array() === typeof new Date()); // true  All types derive from object  Their type is object

Live Demo

 Primitive types are passed by value  When passed as argument  New memory is allocated (in the stack)  The value is copied in the new memory  The value in the new memory is passed  Primitive types are initialized with type literals  Primitive types have a reference type wrapper var number = 5; var text = "Hello there!"; var number = 5; // Holds a primitive value of 5 var numberObj = new Number(5); // Holds a reference value of 5

 Assign string values to two variables  Create an object using their value  Change the value of the variables  Each object has its own value var fname = "Pesho"; var lname = "Ivanov"; var person = {firstName:fname, lastName:lname}; lname = "Petrov"; console.log(person.lastName) // logged "Ivanov"

Live Demo

 Object is the only reference type  When passed to a function the value is not copied, but instead a reference of it is passed var marks= [ {subject : "JavaScript", score : 4.50}, {subject : "JavaScript", score : 4.50}, {subject : "OOP", score : 5.00}, {subject : "OOP", score : 5.00}, {subject : "HTML5", score : 6.00}, {subject : "HTML5", score : 6.00}, {subject : "Photoshop", score : 4.00}]; {subject : "Photoshop", score : 4.00}]; var student = {name:"Doncho Minkov", marks:marks}; marks[2].score = 5.50; console.log(student.marks); // logs 5.50 for HTML5 score

Live Demo

Creating Simple objects

 JSON stands for JavaScript Object Notation  A data format used in JavaScript  Then the object properties can be used: var person = { firstName : "Doncho", firstName : "Doncho", lastName : "Minkov", lastName : "Minkov", toString : function personToString() { toString : function personToString() { return this.firstName + " " + this.lastName; return this.firstName + " " + this.lastName; }} console.log(person.toString()); // writes "Doncho Minkov"

Live Demo

 JSON is great, but repeating code is not, right?  Lets make two persons:  Lots of repeating code  Can't we use a constructor or function to create an object? var minkov = {fname : "Doncho", lname : "Minkov", toString : function(){ return this.fname + " " + this.lname;} toString : function(){ return this.fname + " " + this.lname;}} var georgiev = { fname : "Georgi", lname : "Georgiev", toString : function(){ return this.fname + " " + this.lname;} }

 A function for building JSON objects  Just pass first and last name and get a object  Something like a constructor function buildPerson(fname, lname) { return { return { fname : fname, fname : fname, lname : lname, lname : lname, toString:function (){return this.fname + " " + this.lname;} toString:function (){return this.fname + " " + this.lname;} }} var minkov = buildPerson("Doncho","Minkov"); var georgiev = buildPerson("Georgi","Georgiev");  Much cooler, right?

Live Demo

 JavaScript objects are just a set of key/value pairs  Each value can be accessed by its key  All objects in JavaScript are parsed to JSON  Properties in JSON are accessed using the dot-notation (obj.property)  Yet properties can be used with brackets  Like an array document.write === document["write"] // results in true

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

1. Write functions for working with shapes in standard Planar coordinate system  Points are represented by coordinates P(X, Y)  Lines are represented by two points, marking their beginning and ending  L(P 1 (X 1,Y 1 ), P 2 (X 2,Y 2 ))  Calculate the distance between two points  Check if three segment lines can form a triangle

2. Write a function that removes all elements with a given value  Attach it to the array type  Read about prototype and how to attach methods 3. Write a function that makes a deep copy of an object  The function should work for both primitive and reference types 4. Write a function that checks if a given object contains a given property var arr = [1,2,1,4,1,3,4,1,111,3,2,1,"1"]; arr.remove(1); //arr = [2,4,3,4,111,3,2,"1"]; var obj = …; var hasProp = hasProperty(obj,"length");

5. Write a function that finds the youngest person in a given array of persons and prints his/hers full name  Each person have properties firstname, lastname and age, as shown: 6. Write a function that groups an array of persons by age, first or last name. The function must return an associative array, with keys - the groups, and values - arrays with persons in this groups  Use function overloading (i.e. just one function) var persons = [ {firstname : "Gosho", lastname: "Petrov", age: 32}, {firstname : "Gosho", lastname: "Petrov", age: 32}, {firstname : "Bay", lastname: "Ivan", age: 81},…]; {firstname : "Bay", lastname: "Ivan", age: 81},…]; var persons = {…}; var groupedByFname = group(persons,"firstname"); var groupedByAge= group(persons,"age");