Skip to main content

Teseart interview questions

 1)proxy,proptoype and refelect why to use when json is there?


When working with JSON in JavaScript, you may come across situations where you need to manipulate objects or perform certain operations on them. Let's discuss how the concepts of Proxy, Prototype, and Reflect can be relevant when working with JSON:


Proxy:

The Proxy object in JavaScript allows you to create a proxy for another object, intercepting and customizing its fundamental operations. Proxies are useful when you want to add custom behaviors or validations to an object. When working with JSON, you can use a Proxy to validate or modify JSON data before or after parsing it.


Example:


javascript

Copy code

const json = '{ "name": "John", "age": 30 }';

const validator = {

  set: function(target, property, value) {

    if (property === 'age' && typeof value !== 'number') {

      throw new Error('Age must be a number');

    }

    target[property] = value;

    return true;

  }

};

const parsedData = JSON.parse(json, (key, value) => new Proxy(value, validator));

In the above example, a Proxy is used to validate that the "age" property in the JSON data is a number. If it's not a number, an error is thrown.


Prototype:

Prototypes are a fundamental concept in JavaScript's object-oriented programming model. Every object in JavaScript has a prototype, which is an object from which it inherits properties and methods. When working with JSON, prototypes may not have a direct impact, as JSON represents data in a serialized format without preserving object prototypes.


However, if you want to restore object prototypes after parsing JSON, you can manually assign prototypes to the parsed objects using Object.setPrototypeOf().


Example:


javascript

Copy code

const jsonData = '{ "name": "John", "age": 30 }';

const parsedData = JSON.parse(jsonData);

const person = Object.create(Person.prototype);

Object.assign(person, parsedData);

In this example, after parsing the JSON data, an object person is created with the prototype of a Person constructor. The properties from the parsed data are then assigned to the person object.


Reflect:

The Reflect object provides methods for intercepting and working with JavaScript operations. It provides a more modern and flexible alternative to some of the older reflection capabilities in JavaScript. While working with JSON, the Reflect object may not have a direct application, as JSON operations usually involve parsing and serializing data.


However, in certain scenarios where you need to intercept or manipulate JSON-related operations, such as creating custom JSON parsing behavior or modifying parsed data, the Reflect object can be used along with Proxies to customize the behavior.


Example:


javascript

Copy code

const json = '{ "name": "John", "age": 30 }';

const handler = {

  get: function(target, property) {

    return property in target ? Reflect.get(target, property) : 'Property does not exist';

  }

};

const parsedData = JSON.parse(json, (key, value) => new Proxy(value, handler));

In this example, a Proxy with a handler is used to intercept property access during the JSON parsing process. If a property does not exist in the parsed object, the handler uses the Reflect.get() method to return a custom message instead of undefined.


It's important to note that working with JSON typically involves serialization and deserialization of data, and Proxy, Prototype, and Reflect are more commonly used when dealing with live JavaScript objects. However, they can be applied in specific scenarios where you need to customize or manipulate JSON-related operations or objects.






 2)12 principles of microservice architecture?

Microservice architecture is a software design approach that structures an application as a collection of loosely coupled and independently deployable services. It aims to enhance scalability, agility, and maintainability of the overall system. Here are 12 principles of microservice architecture:

  1. Single Responsibility: Each microservice should have a single responsibility or business capability. It should focus on doing one thing and doing it well.

  2. Autonomous: Microservices should be self-contained and independent. They should have their own data storage, business logic, and deployment process. They can be developed, deployed, and scaled independently of other services.

  3. Decentralized Governance: Decision-making authority and control are distributed among the microservices teams. There is no central authority governing the entire system.

  4. API First: Microservices communicate with each other through well-defined APIs (Application Programming Interfaces). APIs should be designed carefully, emphasizing clarity and ease of use.

  5. Language and Technology Agnostic: Microservices can be implemented using different programming languages, frameworks, and technologies. Each microservice team can choose the best tools for their specific needs.

  6. Resilience: Microservices should be designed to handle failures gracefully. They should be fault-tolerant, isolated, and resilient to network failures and other external dependencies.

  7. Scalability: Microservices allow individual services to be scaled independently based on their specific demand. This ensures efficient resource utilization and improves overall system scalability.

  8. Continuous Integration and Deployment: Each microservice should have its own automated build, test, and deployment pipeline. This enables rapid and independent release cycles for each service.

  9. Infrastructure Automation: Infrastructure provisioning, deployment, and management should be automated using tools like containers, orchestration platforms, and infrastructure as code.

  10. Observability: Microservices should provide mechanisms for monitoring, logging, and tracing to gain insights into their behavior and performance. This helps in debugging, troubleshooting, and performance optimization.

  11. Loose Coupling: Microservices should be loosely coupled to minimize dependencies between services. They should communicate asynchronously and avoid sharing data models or databases.

  12. Domain-Driven Design: Microservices should align with domain boundaries and business capabilities. The design should reflect the domain-specific context and be organized around business concepts.

These principles guide the design and implementation of microservices, promoting modularity, flexibility, and scalability. However, it's important to note that the application of these principles may vary depending on the specific needs and context of each system.


Comments

Popular posts from this blog

Globant part 1

 1)call,apply,bind example? Ans: a. call Method: The call method is used to call a function with a given this value and arguments provided individually. Javascript code: function greet(name) {   console.log(`Hello, ${name}! I am ${this.role}.`); } const person = {   role: 'developer' }; greet.call(person, 'Alice'); // Output: Hello, Alice! I am developer. In this example, call invokes the greet function with person as the this value and passes 'Alice' as an argument. b. apply Method: The apply method is similar to call, but it accepts arguments as an array. Javascript code: function introduce(language1, language2) {   console.log(`I can code in ${language1} and ${language2}. I am ${this.name}.`); } const coder = {   name: 'Bob' }; introduce.apply(coder, ['JavaScript', 'Python']); // Output: I can code in JavaScript and Python. I am Bob. Here, apply is used to invoke introduce with coder as this and an array ['JavaScript', 'Pyt...

Node.js: Extract text from image using Tesseract.

In this article, we will see how to extract text from images using Tesseract . So let's start with this use-case, Suppose you have 300 screenshot images in your mobile which has an email attribute that you need for some reason like growing your network or for email marketing. To get an email from all these images manually into CSV or excel will take a lot of time. So now we will check how to automate this thing. First, you need to install Tesseract OCR( An optical character recognition engine ) pre-built binary package for a particular OS. I have tested it for Windows 10. For Windows 10, you can install  it from here. For other OS you make check  this link. So once you install Tesseract from windows setup, you also need to set path variable probably, 'C:\Program Files\Tesseract-OCR' to access it from any location. Then you need to install textract library from npm. To read the path of these 300 images we can select all images and can rename it to som...

CSS INTERVIEW QUESTIONS SET 2

  You make also like this CSS interview question set 1. Let's begin with set 2, 5)What is the difference between opacity 0 vs display none vs visibility hidden? Property           | occupies space | consumes clicks | +--------------------+----------------+-----------------+ | opacity: 0         |        yes      |        yes       | +--------------------+----------------+-----------------+ | visibility: hidden |        yes       |        no        | +--------------------+----------------+-----------------+ | display: none      |        no       |        no        | When we say it consumes click, that means it also consumes other pointer-events like onmousedown,onmousemove, etc. In e...